Program in c++ to insert a node at the beginning 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 display();
};
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.Display linked
list";
cout<<"\n 3.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<<"\n Display
elements of linked list";
sl.display();
break;
case 3: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::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 nodes at beginning
2.Display linked list
3.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.Display linked list
3.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.Display linked list
3.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.Display linked list
3.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.Display linked list
3.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.Display linked list
3.Exit
Enter your choice:2
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.Display linked list
3.Exit
Enter your choice:3
Exit...