Fixed Linked List

Feb 19, 2014 at 3:38am
Hello there, I'm to trying to build a fixed linked list and I encountered a problem, My linked list only outputs the first element I entered
Something like:
1
2
90 
90


But the desired output is
first displaylist
1
2
3
4
90
100
100
80


second displaylist
1
2
3
4
5
901
100
100
80
70


My code
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
#include <iostream>
#include <conio.h>

using namespace std;

struct Number
{
	int num;
	Number *next; 
};
void linkedList(Number **list, int num)
{
	Number *temp;
	temp=(Number *) malloc(sizeof(Number));
	temp->num=num;
	temp->next=0;
	Number *temp2 = *list;
	if(*list==0)
	{
		*list=temp;
	}
	else
	{
		while(temp2->next!=0)
		{
			temp2->next=temp;
		}
		temp2=temp->next;
		
	}
}
void displayLink(Number **list)
{
	Number *temp=*list;
	while (temp != 0)
	{
		cout << temp->num;
		temp=temp->next;
	}

}
int main ()
{
	Number *list = 0;
	linkedList(&list,90);
	linkedList(&list,100);
	linkedList(&list,100);
	linkedList(&list,80);
	displayLink(&list);
	cout << endl;
	linkedList(&list,70);
	displayLink(&list);
	system ("pause>0");
	return 0;
}


Thank you in advance for any kind help.
Feb 19, 2014 at 7:58am
Look carefully at lines 24-28.
Feb 20, 2014 at 5:30am
I still dont get it.
Feb 20, 2014 at 6:04am
Write down what you think that set of code should be doing, then very slowly and precisely describe what is actually happening on each line. You should notice a difference.
Feb 20, 2014 at 9:56am
1
2
3
4
5
6
while(temp2->next!=0)
		{
			temp2=temp2->next;
		}
		temp2->next=temp;


Thanks
Topic archived. No new replies allowed.