I already asked this question but the code came back with so
I already asked this question but the code came back with so many errors so here it is again.. Oh and it is a C++ quesiton please let me know thank you.
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
Dividing a linked list into two sublists of almost equal sizes:
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;
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.
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
Dividing a linked list into two sublists of almost equal sizes:
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;
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.
Solution
#include iostream.h
int main()
{
mylist.push_back(12);
mylist.push_back(10);
void divideMid(linkedListType<Type> &sublist);
template<class Type>
void linkedListType<Type>::divideMid(linkedListType
<Type> &sublist)
{
nodeType<Type> *current;
nodeType<Type> *mid;
if (first == NULL)
{
sublist.first = NULL;
sublist.last = NULL;
sublist.count = 0;
}
else
if (first->link == NULL)
{
sublist.first = NULL;
sublist.last = NULL;
sublist.count = 0;
}
else
{
mid = first;
current = first->link;
if (current != NULL)
current = current->link;
int i = 1;
while (current != NULL)
{
mid = mid->link;
current = current->link;
i++;
if (current != NULL)
current = current->link;
}
sublist.first = mid->link;
sublist.last = last;
last = mid;
last->link = NULL;
sublist.count = count - i;
count = i;
}
}



