Program in c++ to insert a node at the end of linked list.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node*next;
}*start;
class single_llist
{
public:
node*create_node(int);
void insert_begin();
void insert_last();
void display();
single_llist()
{
Start=NULL;
}
};
main()
{
int choice,nodes,element,position,i;
single_llist sl;
start=NULL;
clrscr();
while(1)
{
cout<<"\n -------------------------------";
cout<<"\n Operation on single
linked list";
cout<<"\n
-------------------------------";
cout<<"\n 1.Insert nodes at
beginning";
cout<<”\n 2.Insert node at last”;
cout<<"\n 3.Display linked
list";
cout<<"\n 4.Exit";
cout<<"\n Enter your
choice:";
cin>>choice;
switch(choice)
{
case 1:cout<<"\n Inserting
nodes at Beginning:";
sl.insert_begin();
break;
case 2:cout<<Insert node at last”;
sl.insert_last();
break;
case 3:cout<<"\n Display
elements of linked list";
sl.display();
break;
case 4:cout<<"\n
Exit...";
exit(1);
break;
default:cout<<"\n Wrong
choice";
}
}
}
node*single_llist::create_node(int value)
{
struct node*temp,*s;
temp=new(struct node);
if(temp==NULL)
{
cout<<"\n Memory not
allocated";
return 0;
}
else
{
temp->info=value;
temp->next=NULL;
return temp;
}
}
void single_llist::insert_begin()
{
int value;
cout<<"\n Enter the value to be
inserted:";
cin>>value;
struct node*temp,*p;
temp=create_node(value);
if(start==NULL)
{
start=temp;
start->next=NULL;
}
else
{
p=start;
start=temp;
start->next=p;
}
cout<<"\n Element inserted at
beginning";
}
void single_llist::insert_last()
{
Int value;
cout<<”\n Enter the value to be
inserted”;
cin>>value;
struct node*temp,*s;
temp=create_node(value);
s=start;
while(s->next=NULL)
{
s==s->next;
}
temp-next=NULL;
s->next=temp;
cout<<”\n Element inserted at
last”<<endl;
}
void single_llist::display()
{
struct node*temp;
if(start==NULL)
{
cout<<"\n The list is empty"<<endl;
return;
}
temp=start;
cout<<"\n Element of list
are:";
while(temp!=NULL)
{
cout<<temp->info<<"->";
temp=temp->next;
}
cout<<"NULL";
}
OUTPUT:
-----------------------------------------
Operation on single linked list
-----------------------------------------
1.Insert node at beginning
2.Insert node at last
3.Display linked list
4.Exit
Enter your choice:1
Inserting nodes at Beginning:
Enter the value to be inserted:10
Element inserted at beginning
-----------------------------------------
Operation on single linked list
-----------------------------------------
1.Insert nodes at beginning
2.Insert
node at last
3.Display
linked list
4.Exit
Enter your choice:1
Inserting nodes at Beginning:
Enter the value to be inserted:20
Element inserted at beginning
-----------------------------------------
Operation on single linked list
-----------------------------------------
1.Insert nodes at beginning
2.Insert node at last
3.Display linked list
4.Exit
Enter your choice:1
Inserting nodes at Beginning:
Enter the value to be inserted:30
Element inserted at beginning
-----------------------------------------
Operation on single linked list
-----------------------------------------
1.Insert nodes at beginning
2.Insert node at last
3.Display linked list
4.Exit
Enter your choice:1
Inserting nodes at Beginning:
Enter the value to be inserted:40
Element inserted at beginning
----------------------------------------
Operation on single linked list
-----------------------------------------
1.Insert nodes at beginning
2.Insert
node at last
3.Display
linked list
4.Exit
Enter your choice:1
Inserting nodes at Beginning:
Enter the value to be inserted:50
Element inserted at beginning
-----------------------------------------
Operation on single linked list
-----------------------------------------
1.Insert nodes at beginning
2.Insert
node at last
3.Display
linked list
4.Exit
Enter your choice:2
Insert node
at last
Enter the
value to be insertrd 5
Element
inserted at last
-----------------------------------------
Operation on single linked list
-----------------------------------------
1.Insert nodes at beginning
2.Insert
node at last
3.Display
linked list
4.Exit
Enter your choice:3
Display elements of linked list
Element of list
are:50->40->30->20->10->NULL
-----------------------------------------
Operation on single linked list
-----------------------------------------
1.Insert nodes at beginning
2.Insert node at last
3.Display
linked list
4.Exit
Enter your choice:4
Exit...