Double Linked List Issue

I'm having issues with this program. When I enter more than 2 extra I.d's and try to use the display function it'll only display the head and the tail of the linked list. It's almost as if the middle links have completely disappeared.

Last edited on
bump*
Line 80: A newly created node that should be added is check for the previous node?
Line 82: You replace the passed node with an again newly created node (memory leak).

The else in addSong creates two new nodes which doesn't make sense at all.
How do you suggest I fix this then? I'm stumped..
bump - Can't fix this program.
You're doing it way to complicated:
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
void addSong(SongDoublyLinkedList *songList);
void displayListElements(SongDoublyLinkedList *songList);

int main()
{
	SongDoublyLinkedList songList;
	songList.firstElement = NULL; // Note: Important!
	bool question = true;
	while(question == true)
	{
		char letter;
		cout << "Enter the number of what you'd like to do next: " << endl;
		cout << " a = Add a New Song" << endl;
		cout << " b = Display List of Songs" << endl;
		cout << " c = Terminate Program" << endl;
		cin >> letter;
		switch(letter)
		{
			case 'a':
				{ 
					addSong(&songList);
					break;
				}
				
				
				
			case 'b': 
				{
					displayListElements(&songList);
					break;
				}
			
			case 'c': 
				{
					question = false;
					break;
				}
				
		}
	}
	return 0;
	
	
	
	
}

void addSong(SongDoublyLinkedList *songList)
{
	SongNode *songTemp = new SongNode;
	songTemp->previousNode = NULL; // Note: Important!
	songTemp->nextNode = NULL; // Note: Important!
	cout << "Enter The New Song's ID: ";
	cin >> songTemp->sg.id;
	
	if(songList->firstElement)
	{
		SongNode *temp = songList->firstElement;
		while(temp)
		{
			if(temp->nextNode)
				temp = temp->nextNode;
			else
			{
				songTemp->previousNode = temp;
				temp->nextNode = songTemp;
				break;
			}
		}
	}
	else
	{
		songList->firstElement = songTemp;
	}
	
}


Better make constructors for the structs.
Thank-You so much! This has really helped out a lot! I didn't realize that I really only needed to pass that one structure through the functions.
Topic archived. No new replies allowed.