#include <iostream>
#include <conio.h>
usingnamespace 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*/
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.
#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*/
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
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.
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.