error working with linked list in c++

Pages: 12
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <conio.h>

using namespace std;
class node
{
	public:
	int data;
	node *link;
}*start,*last;

void initialize()
	{
    start=new node;
    last=new node;
	}

void ins_at_beg()
	{
	node *temp=new node;
	cout<<"ENTER THE DATA"<<endl;
	cin>>temp->data;
	
	if(start==NULL)
		{
		start->data=temp->data;
		last->data=temp->data;
		last=temp;
		last->link=NULL;
		}
	else
		{
		temp->link=start;
		start=temp;
		}
	}
	
void ins_at_end()
	{
	node *temp=new node;
	cout<<"ENTER THE DATA"<<endl;
	cin>>temp->data;
	
	if(last==NULL)
		{
		start=temp;
		last=temp;
		last->link=NULL;
		}
	else
		{
		last->link=temp;
		last=temp;
		last->link=NULL;
		}
	}
	
void del_at_beg()
	{
	if(start=NULL)
		cout<<"UNDERFLOW"<<endl;
	else
		start=start->link;
	}

// void del_at_end()
	// {
	
	// }
	
void traverse()
	{
	node *temp;
	temp=start;
	while(temp->link!=NULL)
		{
		cout<<temp->data;
		temp=temp->link;
		if(temp->link==NULL)
			cout<<temp->link;
		}
	}

int main()
	{
	int choice;
	do
	{
	cout<<"ENTER YOUR CHOICE"<<endl
		<<"\t1. INSERT AT BEGGINING"<<endl
		<<"\t2. INSERT AT END"<<endl
		<<"\t3. DELETE AT BEGGINING"<<endl
		<<"\t4. DELETE AT BEGGINING"<<endl;
	cin>>choice;
	
	switch(choice)
		{
		case 1:
			ins_at_beg();
			break;
		case 2:
			ins_at_end();
			break;
		case 3:
			del_at_beg();
			break;
		// case 4:
			// del_at_end();
			// break;
		case 5:
			traverse();
			break;
		}

	cout<<"ENTER 1 TO CONTINUE";
	cin>>choice;
	}
	while(choice==1);
	getch();
	}
/*After I run the program and enter 1 as the choice it asks me for the data
I enter it and then it says linked list.exe has stopped working.
Also tried on linux using g++ but gave Segmentation Fault 
after the above mentioned step
Thank you in advance*/
Last edited on
1
2
3
if(start==NULL)
{
	start->data=temp->data;

You are dereferencing a null pointer.
but data is not a pointer. please explain me if i'm wrong
No but start is a pointer.
ok so i need to change start->data to *start->data
please notify me about the exact replacement i'm new to c++
please write to me the correction thanks in advance
You need to make start point to temp. You do it correctly in ins_at_end() so I think you should be able to figure it out.
thank you @Peter87 i figured out the thing can you me with one more thing
if i have start=NULL;
and then i want to make any changes just to the data of start or say i want to assign a value to start->data then how to do so.
Thank you once more to help me out.
You can't. start == NULL means that start does not point to anything.
ok thanks i updated the program according to you but still the problem persist
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream.h>
#include <conio.h>


class node
{
	public:
	int data;
	node *link;
}*start,*last;

void initialize()
	{
    start=last=NULL;
	}

void ins_at_beg()
	{
	node *temp;
	cout<<"ENTER THE DATA"<<endl;
	cin>>temp->data;
	if(start==NULL)
		{
		start=temp;
		last=temp;
		}
	else
		{
		temp->link=start;
		start=temp;
		}
	}
	
void ins_at_end()
	{
	node *temp;
	cout<<"ENTER THE DATA"<<endl;
	cin>>temp->data;
    if(last=NULL)
		{
		start=temp;
		last=temp;
		}
	else
		{
		last->link=temp;
		last=temp;
		}
	}
	
void del_at_beg()
	{
	if(start=NULL)
		cout<<"UNDERFLOW"<<endl;
	else
		start=start->link;
	}

void del_at_end()
	{
	node *temp=start;
	node *pred;
	while(temp->link!=NULL)
	{
	pred=temp;
	temp=temp->link;
	}
	pred->link=NULL;
	}
	
void traverse()
	{
	node *temp=start;
	while(temp->link!=NULL)
		{
		cout<<temp->data;
		temp=temp->link;
		if(temp->link==NULL)
			cout<<temp->data;
		}
	}

int main()
	{
	int choice;
	do
	{
	cout<<"ENTER YOUR CHOICE"<<endl
		<<"\t1. INSERT AT BEGGINING"<<endl
		<<"\t2. INSERT AT END"<<endl
		<<"\t3. DELETE AT BEGGINING"<<endl
		<<"\t4. DELETE AT BEGGINING"<<endl;
	cin>>choice;
	
	switch(choice)
		{
		case 1:
			ins_at_beg();
			break;
		case 2:
			ins_at_end();
			break;
		case 3:
			del_at_beg();
			break;
		case 4:
			del_at_end();
			break;
		case 5:
			traverse();
			break;
		}

	cout<<"ENTER 1 TO CONTINUE";
	cin>>choice;
	}
	while(choice==1);
	getch();
	}
*After I run the program and enter 1 as the choice it asks me for the data
I enter it and then it says linked list.exe has stopped working.
Also tried on linux using g++ but gave Segmentation Fault 
after the above mentioned step
Thank you in advance*/
You still need to create the node.
node *temp=new node;
how do i do for last->link in ins_at_end. suggest me an alternative please
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void ins_at_end()
	{
	node *temp;
	cout<<"ENTER THE DATA"<<endl;
	cin>>temp->data;
    if(last=NULL)
		{
		start=temp;
		last=temp;
		}
	else
		{
		last->link=temp;
		last=temp;
		}
	}
when i call ins_at_end function the same problem occurs program has stopped working. help @Peter87
I think ins_at_end() was working from the start so I don't get why you are changing it.
i did the following now help me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void ins_at_end()
	{
	node *temp=new node;
	cout<<"ENTER THE DATA"<<endl;
	cin>>temp->data;
    	if(start=NULL)
		{
		temp->link=NULL;
		start=temp;
		}
	else
		{
		node *temp2=new node;
		temp2=start;
		while(temp2->link!=NULL)
			{
			temp2=temp2->link;
			}
		temp2->link=temp;
		temp->link=NULL;
		}
	}
	

still getting the same problem because of temp->link;
I still don't get why you make changes to ins_at_end() when it was ins_at_beg() that had problems. Was there really any problems with the original ins_at_end()? I don't see any problems with it.

I think you can reuse the original ins_at_end() code to implement ins_at_beg(). You only have to replace
1
2
3
last->link=temp;
last=temp;
last->link=NULL;
with some other lines of code.
you not getting it there was problem with the orignal ins_at_end as well as ins_at_beg. As you can see last->link=temp in your latest reply would lead to deferencing of null pointer last and that's why program ends into segmentation fault. nyways thanks for the help till now the rest three functions work like a charm. please help me with the ins_at_end().
As you can see last->link=temp in your latest reply would lead to deferencing of null pointer last and that's why program ends into segmentation fault.

No. That code will never run when last is null because of the surrounding if-else statement.
but i tried checking it out it didn't work. can u help me correct just the following cod leaving the rest i'll be grateful

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void ins_at_end()
	{
	node *temp=new node;
	cout<<"ENTER THE DATA"<<endl;
	cin>>temp->data;
    	if(start=NULL)
		{
		temp->link=NULL;
		start=temp;
		}
	else
		{
		node *temp2=new node;
		temp2=start;
		while(temp2->link!=NULL)
			{
			temp2=temp2->link;
			}
		temp2->link=temp;
		temp->link=NULL;
		}
	}

can u please help me in half an hour with this function i need to show the thing tomorrow morning i.e just 6 hours remaining please the above code
Last edited on
I'm sorry don't see what's wrong with ins_at_end() in your original code. I thought you could just copy the ins_at_end() code to ins_at_beg() with the only modification in ins_at_beg() being the else part changed to
1
2
temp->link = start;
start = temp;
It appears to work for me. Well, I can't see that the nodes are inserted correctly but at least it doesn't crash any more when inserting numbers from the menu. If that still isn't right I don't know what to do, maybe someone else can take a look at this with a fresh pair of eyes.

I see in you last attempts you are no longer using the last pointer. Is that because you have decided to not have a last pointer any more. Fine, you don't need a last pointer but you could have told me if that was the reason the original code has to be changed. The problems I see with the latest code you posted is that you use = on line 6 instead of ==. You are also creating a new node on line 13 for no reason.
Pages: 12