Double Linked Lists

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
1
2
3
4
5
6
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...
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
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 deletes, 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:
1
2
3
4
5
6
7
8
9
10
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.

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.
}

int main()
{
//Get the number and test it
if(even)
AddNum(EvenList, num);
else
AddNum(OddList, num);
}
Topic archived. No new replies allowed.