i think i have some problem with my divideAt fucntion in my header file im pretty sure im doing something wrong if anyone could help please thank and i kept getting the wrong output for my answer which the out put should be
Enter numbers ending with -999
22 34 56 2 89 90 0 14 56 11 43 55 -999
Enter the number at which to split list: 0
list and otherList after splitting at 0
list: 22 34 56 2 89 90
Length of list: 6
otherList: 0 14 56 11 43 55
Length of subList: 6
{ //main
int num;
//int num2;
unorderedLinkedList<int> myList;
unorderedLinkedList<int> secondList;
cout << "enter some number and end it with -999 to quit enter numbers " << endl;
cin >> num;
while(num != -999)
{
myList.insertLast(num);
cin >> num;
}
cout << "here is the list of inters in the order you have gave them: \n";
myList.print();
cout << endl;
//cout << "enter the number you wish to split the list at: \n";
//cin >> num2;
myList.divideAt(secondList,0);
cout << "list and otherList after splitting at 0: \n";
myList.print();
cout << endl;
cout << "length of the list: " << myList.length();
cout << endl;
cout << "other list: \n";
secondList.print();
cout<< endl;
cout << "length of the sublist: " << secondList.length();
return 0;
}
im not really understanding what you are saying is
I wrote:
What about the rest of the list?
Make a function that rather than splitting the list does print the "info" for each.
Then modify it so that it prints only up to the element, where info==item.
Is that code even remotely similar to your divideAt?
To help clarify. The main problem with your divideAt() function is the failure to iterate through the list to find the node with info==item.
In your code only the 2nd node in the list is examined:
1 2 3 4 5 6 7 8 9 10 11
numTrail =first;
current =first->link;// current points to 2nd node in list
if(current->info != item)// 2nd node only examined
{
numTrail =current;
current = current ->link;
}
elseif(current->info==item)
numTrail->link =NULL;
secondList.first=current;
You need to search the list for the node with info==item.
Actually, you're very close with the code. Change line 18 from if(current->info != item) to while(current->info != item)
then remove line 23. The assignments on lines 24, 25 look correct. The code should work.
The only added thing I'd do is watch for an item given which is not in the list. You will iterate right off the end making current=NULL then this: while(current->info != item) will cause a crash. Check for NULL during iteration and the result: