help with my list for divideAt

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//header file part
template<class Type>
void linkedListType<Type>::divideAt(linkedListType<Type>  &secondList, const  Type&  item)
{
     //variable
     nodeType<Type>* numTrail;
     nodeType<Type>* current;

     if(first ==NULL)
        cout << "there's nothing in the list.\n";
     else if(first->link ==NULL)
        cout << "one item in this list and not dividing";
     else
     {
         numTrail =first;
         current =first->link;

         if(current->info != item)
         {
             numTrail =current;
             current = current ->link;
         }
         else if(current->info==item)
              numTrail->link =NULL;
               secondList.first=current;


     }

}






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{    //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;
}
1
2
3
4
5
6
7
8
9
10
11
numTrail =first;
current =first->link;

if ( current->info != item ) // looking at first->link->info
{
  numTrail =current;
  current = current ->link;
}
else if ( current->info==item ) numTrail->link =NULL;

secondList.first=current;

What about the rest of the list?

What is the third option? (You do check for == and != ) Wouldn't if .. else suffice?

What is in the body of the second if?
im not really understanding what you are saying is
are u asking for the rest of the header file am i missing information??
warrior757 wrote:
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?
closed account (D80DSL3A)
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;
}
else if(current->info==item)
      numTrail->link =NULL;
      secondList.first=current;

You need to search the list for the node with info==item.
how would i do that and would it be the same as the dividemid because i had to write that one also
this is the only part of my logic that is wrong correct?
closed account (D80DSL3A)
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:
1
2
3
4
5
6
7
8
9
while(current != NULL && current->info != item)
{
    // iterate
}

if( current != NULL )
{
    // make assigns
}

line 23 ,24,25 which one are those
nevermind thank you for the help i figure it out and you help alot
Topic archived. No new replies allowed.