Question

C++ Programming Code Do not change anything in the supplied code below that will be the...

C++ Programming Code

Do not change anything in the supplied code below that will be the Ch16_Ex5_MainProgram.cpp except to add documentation and your name.

  1. Please use the file names listed below since your file will have the following components:

    Ch16_Ex5_MainProgram.cpp - given file


    //22 34 56 4 19 2 89 90 0 14 32 88 125 56 11 43 55 -999

    #include <iostream>
    #include "unorderedLinkedList.h"

    using namespace std;

    int main()
    {
    unorderedLinkedList<int> list, subList;

    int num;                                                                                                                       

                cout << "Enter numbers ending with -999" << endl;                                                                                                                     
    cin >> num;                                                                                                     

                while (num != -999)                                                                                          
    {
    list.insertLast(num);                                                                  
    cin >> num;                                                                                                     
    }

                cout << endl;                                                                                                   

                cout << "List: ";                                                
    list.print();                                                                                                        
    cout << endl;                                                                                                               
    cout << "Length of the list: " << list.length() << endl;                                                                                                      

                list.divideMid(subList);

                cout << "Lists after splitting list" << endl;

                cout << "list: ";
    list.print();                                                                                                        
    cout << endl;                                                                                                               
    cout << "Length of the list: " << list.length() << endl;                  

                cout << "sublist: ";
    subList.print();                                                                                                  
    cout << endl;                                                                                                               
    cout << "Length of subList: " << subList.length() << endl;
    system("pause");
    return 0;                                                          
    }

    linkedList.h
    unorderedLinkedList.h

  2. Dividing a linked list into two sublists of almost equal sizes:
    1. Add the operation divideMid to the class linkedListType as follows:

      void divideMid(linkedListType &sublist);
      //This operation divides the given list into two sublists
      //of (almost) equal sizes.
      //Postcondition: first points to the first node and last
      //          points to the last node of the first
      //          sublist.
      //          sublist.first points to the first node
      //          and sublist.last points to the last node
      //          of the second sublist.
      Consider the following statements:

      unorderedLinkedList <int> myList;
      unorderedLinkedList <int> subList;

    2. Suppose myList points to the list with elements 34 65 27 89 12 (in this order). The statement:

      myList.divideMid(subList);

      divides myList into two sublists: myList points to the list with the elements 34 65 27, and subList points to the sublist with the elements 89 12.

      Write the definition of the function template to implement the operation divideMid. Also, write a program to test your function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, since you have not provided "unorderedlinkedlist.h" file i have to write code with assumption that unorderedlinkedlist.h has a class which gives implementation of unorderedLinkedList class and unorderedLinkedList class contains a headPtr member which points to the head of linkedList and each member in the linked list has a "next" pointer which points to the next node in the linked list

Based on above assumption here is the implementation of divideMid(linkedListType &sublist); method

void divide(linkedListType &subList)
{
//please change the below line based on your class structure
//logic of program will remain the same
Node *temp = this.headPtr;
int len=0;//finding the length of the linkedListType
while(temp!=NULL)
{
len++; //incrementing for length
temp=temp->next;
}
//ceil function is present in cmath library
// using it to find if length is odd number
int mid = ceil((float)len/2);
temp=this.headPtr;
int i=1; //counter till the mid node
//itearting the list till we find the middle node
while(temp!=NULL && i<mid )
{
i++;
temp=temp->next;
}
//setting mid to be the head of new linked list
subList.head=temp;
//setting the mid to be the end of first linked list
temp->next=NULL;
}

Add a comment
Know the answer?
Add Answer to:
C++ Programming Code Do not change anything in the supplied code below that will be the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #C++, Programming: Program Design Including Data Structures, 7th Edition, it is too long so i can't...

    #C++, Programming: Program Design Including Data Structures, 7th Edition, it is too long so i can't add the these two file.(unorderedlinked.h and linkedlist.h) as well as the output Please use the file names listed below since your file will have the following components: Ch16_Ex5_MainProgram.cpp - given file //22 34 56 4 19 2 89 90 0 14 32 88 125 56 11 43 55 -999 #include #include "unorderedLinkedList.h" using namespace std; int main() { unorderedLinkedList list, subList; int num;                                                                                                                                   ...

  • PLEASE FILL THE CODE ACCORDINGY AND THE REQUIRED OUTPUT IS GIVEN BELOW ALONG WITH THE INPUT...

    PLEASE FILL THE CODE ACCORDINGY AND THE REQUIRED OUTPUT IS GIVEN BELOW ALONG WITH THE INPUT Problem: The following function, buildListForward, builds a linked list in a forward manner and returns the pointer of the built list: Q1: The bulidListForward function to create a linked List structure with the keyboard input( cin >> num). Change this function to receive the values stored in the array from the main function( use int type pointer variable). eg. nodeType* buildListForward(int *arrayPrt, int Size)...

  • I have a problem with merging the two linked lists together. In the function AscendMerge, I...

    I have a problem with merging the two linked lists together. In the function AscendMerge, I am trying to compare the values of each node in the two lists and output them into one list in ascended order. Everything works except for the one function. Can someone tell me what im doing wrong, when I run it it skips over values. #include <iostream> #include <cassert> using namespace std; struct nodeType {    int info;    nodeType *link;    nodeType *current;...

  • Do not change anything in the supplied code below that will be the Ch12_Ex9.cpp except to...

    Do not change anything in the supplied code below that will be the Ch12_Ex9.cpp except to add documentation and your name. Please use the file names listed below since your file will have the following components: Ch12_Ex9.cpp given file //Data: 18 42 78 22 42 5 42 57 #include <iostream> #include "unorderedArrayListType.h" using namespace std; int main() { unorderedArrayListType intList(25);     int number;     cout << "Enter 8 integers: ";     for (int count = 0; count < 8; count++)...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • Linkedlist implementation in C++ The below code I have written is almost done, I only need...

    Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() functio​n. ​ Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...

  • C++ assignment about doubly linked list

    You are required to write the following functions using this class: class Doubly_linked_list // Use a class Doubly_linked_list to represent an object {     public:     // constructor initialize the nextPtr     Doubly_linked_list()     {         prevPtr = 0; // point to null at the beginning         nextPtr = 0; // point to null at the beginning     }     // get a number     int GetNum()     {         return number;     }     // set a number     void SetNum(int num)     {         number = num;     }     // get the prev pointer     Doubly_linked_list ...

  • This is just a partial C++ code. I am having a problem with the getline(cin, variable)...

    This is just a partial C++ code. I am having a problem with the getline(cin, variable) statement of my code. Please run the code for yourself and see that it doesn't let you enter the full name. It skips the prompt to enter the name and goes straight to the next cout statement. I can't use cin alone because I need to capture and store both the first name followed by space followed by last name. I was trying to...

  • a7q3.cc File #include <iostream> #include <cstring> #include "ArrayList.h" using namespace std; // Algorithm copy(s) // Pre:...

    a7q3.cc File #include <iostream> #include <cstring> #include "ArrayList.h" using namespace std; // Algorithm copy(s) // Pre: s :: refToChar // Post: memory allocated on heap to store a copy // Return: reference to new string char *copy(char *s) { char *temp = new char[strlen(s)+1]; strcpy(temp,s); return temp; } void test_ListOperations(){    cout << "testing createList" << endl;    List *myList = createList(10);    if (myList == NULL){ cout << "createList failed" << endl; return; } else{ cout << "createList succeeded"...

  • Using C++ 11, Create the following function based on the criteria mentioned below: node.hpp             ○This...

    Using C++ 11, Create the following function based on the criteria mentioned below: node.hpp             ○This will contain your ​node struct○value ​is type int             ○next​ is type Node*             ○(optional) ​prev​ is type Node* nodeFunctions.hpp           ○This will contain the headers for all the functions you need to display, add, and remove                nodes from your list             ■displayList ­ displays the entire list from beginning to end. If no elements let the user       know the list is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT