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
 ``123456`` ``````int num; fileIn>>num; if(num%2 == 0) EvenList.push_back(num); else OddList.push_back(num);``````
I am unfamiliar with that instruction...what does that do? I am working with double linked lists...
Doubly linked lists? As in each node points both to the node ahead and behind it?
Do you have two linked lists, one for odd and one for even numbers?

My code reads the number in, checks if it's even, and then adds it to the corresponding list. Which instruction are you unfamiliar with?
Hmm...yeah I see what you are doing now. I have never seen the .push_back () instruction before.

This is what I came up with, but I am unsure if it is correct...
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108`` ``````#include "stdafx.h" #include #include #include #include #include using namespace std; // struct definition for even number linked list node struct evenListNode { evenListNode () { evenListNext = NULL; evenListPrev = NULL; } int evenNum; evenListNode* evenListNext; evenListNode* evenListPrev; }; // struct definition for odd number linked list node struct oddListNode { oddListNode () { oddListNext = NULL; oddListPrev = NULL; } int oddNum; oddListNode* oddListNext; oddListNode* oddListPrev; }; int main () { int data; evenListNode* evenHead = NULL; oddListNode* oddHead = NULL; cout << "Double Linked List Program" << endl; ifstream file_A; file_A.open ("listnums.txt"); if (!file_A) { cout << "The file does not exist!" << endl; } while (file_A.good () && file_A.peek () != EOF) { evenListNode* evenTmp = new evenListNode (); oddListNode* oddTmp = new oddListNode (); file_A >> data; if (data % 2 == 0) { evenTmp -> evenNum = data; if (evenTmp != NULL) { evenHead = evenTmp; } else { evenListNode* iter = evenHead; while (iter -> evenListNext != NULL) { iter = iter -> evenListNext; } iter -> evenListNext = evenTmp; } } else { oddTmp -> oddNum = data; if (oddTmp != NULL) { oddHead = oddTmp; } else { oddListNode* iter = oddHead; while (iter -> oddListNext != NULL) { iter = iter -> oddListNext; } iter -> oddListNext = oddTmp; } } } system ("PAUSE"); return 0; }``````
Of course, these are broken down into separate individual functions...i just put everything together to make it a bit easier to read
You don't need two different data definitions for even and odd lists. It's the exact same structure. You also `new` a bunch of stuff but I don't see any `delete`s, which will be a memory leak.

Are you allowed to use member functions/classes? That's what `EvenList.push_back(num)` is. It's calling a function of the list class on the list object "EvenList".
Last edited on
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.

Also, I am not allowed to use OOP, that's it.
Define "LinkedListNode" once, and then declare two objects:
 ``12345678910`` ``````struct LinkedListNode { //Data }; int main() { LinkedListNode EvenNums; LinkedListNode OddNums; }``````

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.

 ``1234567891011121314`` ``````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. } int main() { //Get the number and test it if(even) AddNum(EvenList, num); else AddNum(OddList, num); }``````
Topic archived. No new replies allowed.