Anyone have any advice on how to obtain data from a .txt file, determine if it is even/odd and then store it into 1 of 2 linked lists depending on whether it is even or odd?
So far, I can figure out how to do it with ONE linked list, but I cannot figure out how to do it with two separate ones. The thing that is killing me right now is:
I set up the node* tmp = new evenNode for the even list.
then i read the data in like: fileIn >> tmp->evenNode
from there, I make an if statement to determine whether the data is even or odd, and then I add to the list...when I output the list, it shows all even numbers, which is what I would expect.
What I cannot figure out how to do is the above part...but using the odd list..
does it matter if i create the tmp place on the even or odd list? Any advice, websites that show some examples, etc etc would be greatly appreciated!!
Is there a way to read the data in first, then create the if statement to determine whether it is even or odd, and THEN tell it where to go?? I think the tmp node is confusing me...maybe i dont understand linked lists as well as i thought
What do you mean I don't need two different data definitions? As in, I don't need to define both the evenList and the oddList? How will my program know where to put the data then once it determines if it is odd or even if i only define one list?
I don't have any reason to delete anything yet, so I haven't added that step.
About the memory leak, you create two new temporary nodes during each iteration but only use one of them each time. The other is a memory leak.
Okay, so what you could do is write a function that takes a root-node of a list and a number and adds that number to the end of that list.
Then you can basically use the code I gave you to add the number to either the even or the odd list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void AddNum(LinkedListNode root, int num)
//Code to add a node with the data "num" to the end of the list that
//is described by the root-node root.
//Get the number and test it