ordered doubly linked list

hey everyone.
I'm writing a code to read student information from file and store them in an ordered doubly linked list.
the list should be ordered according to the student ID.
The information in the file aren't ordered.

I had a problem in the function which reads from file and store the information in the list
i tried to separate them in two functions , it didn't work .
So any suggestions to solve the problem??
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
void list::generateList(ifstream &inFile, char fileName[20])
{
	string name1 , name2 ,name3 ;
	int id ,Grade ,birth;
	node *temp;
	node *curr;
	node *prev;
	inFile.open(fileName,ios::in);
	if(!inFile)
	{
		cerr<<" Unable to open the file "<<endl;
		exit(1);
    }
	
	bool found = false;
	while(!inFile.eof())
	{	
		temp = new node;
		inFile>>id
			  >>name1
			  >>name2
			  >>name3
			  >>birth
			  >>Grade;
			temp->set(id , name1,name2,name3 ,birth,Grade);
			temp=temp->next;

		if (first==NULL)
	{
		first = temp;
		last = temp;
		counter++;
	}
	else
	{
		curr= first;
		while (!found && curr)
		{
			if (curr->getID() >= temp->getID())
				found = true;
			else
			{
				prev =curr;
				curr=curr->next;

			}
		}
		if (curr==first)
		{
			temp->next = curr;
			first->back= temp;
			first=temp;
		}
		else if (curr)
		{
			prev->next = temp;
			temp->back=prev;
			temp->next=curr;
			curr->back=temp;
		}
		else 
		{
			prev->next=temp;
			temp->back=prev;
			last=temp;
		}

	}
	counter++;
		}
	inFile.close();
	}
Do you have to use a DLL? (Double linked-list). It'd be a lot easier to store them in a map using student id as key, as this is naturally ordered.

Regarding your actual code, some basic commenting would've hurt. You should also put braces around the statement on Line 40. If one execution path of a conditional statement has braces then so should the others, it aids in readability.

Line 26: Why are you moving your only valid pointer to the new Node to it's Next Object? Unless Next currently points to "this"; in which case it'd still be pointless. You're also not re-setting your "found" variable to false after each attempt. Correcting those should point you in the right direction.
Thanks for you help
I will consider your notes
Topic archived. No new replies allowed.