I have an assignment where I have to split the linked list at a given position by the user.
The linked list looks as such:
15 -> 0 -> 15 -> 10 -> 20 -> 30 -> 60 -> NULL;
User wants to split the link at node 3:
15 -> 0 -> 15 -> NULL;
10 -> 20 -> 30 -> 60 -> NULL;
The problem arises when I ask the user to enter what node to split it, the output is always 15. So it's always showing the very first node and deleting everything else
I cannot use deep copy to copy the new link into a whole new node, I have to do this using pointers only and delinking/re-linking
Your professor would like to see two lists when you are done, and for you to print them both out. Your code should look something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
TheNode* one = new TheNode(10);
one->add(20);
one->add(30);
...
// Split the first list into two lists
TheNode* two = one.split(pos);
// Display the two lists
one.display();
two.display();
// Don't forget to clean up!
one.free();
two.free();
Therefore, the method should have the following prototype:
TheNode* TheNode::split(int pos)
The function is being asked to do two things. Find a position and separate two nodes. I would personally have another function which simply finds an indexed node: