Program in c++ to deleted a node at the beginning, at the end and in the middle of the given linked list
Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node*next;
}*start;
class single_llist
{
clrscr();
public:
node*create_node(int);
void insert_begin();
void delete_pos();
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 operations
on single linked list";
cout<<"\n----------------------------------";
cout<<"\n 1.insert
node at beginning";
cout<<"\n 2.delete a
particular node";
cout<<"\n 3.display
linked list";
cout<<"\n
4.exit";
cout<<"\n enter your
choice:";
cin>>choice;
switch(choice)
{
case 1:cout<<"\n
inserting node at beginning:";
sl.insert_begin();
break;
case 2:cout<<"\n
reverse element of linked list";
sl.delete_pos();
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::delete_pos()
{
int pos,i,counter=0;
if(start==NULL)
{
cout<<"\n list is
empty";
return;
}
cout<<"\n enter the
position of value to be deleted:";
cin>>pos;
struct node *s,*ptr;
s=start;
if(pos==1)
{
start=s->next;
}
else
{
while(s!=NULL)
{
s=s->next;
counter++;
}
if(pos>0 &&
pos<=counter)
{
s=start;
for(i=1;i<pos;i++)
{
ptr=s;
s=s->next;
}
ptr->next=s->next;
}
else
{
cout<<"\n position
out of range";
}
free(s);
cout<<"\n element
deleted...";
}
}
void single_llist::display()
{
struct node*temp;
if(start==NULL)
{
cout<<"\n the list
is empty"<<endl;
return;
}
temp=start;
cout<<"\n elements of
list are:";
while(temp!=NULL)
{
cout<<temp->info<<"->";
temp=temp->next;
}
cout<<"NULL";
}
Output:
------------------------------------------
operations on single linked list
--------------------------------------------
1.insert node at beginning
2.insert node at given position
3.display linked list
4.exit
enter your choice:1
inserting node to be inserted:10
element inserted at beginning
------------------------------------------
operations on single linked list
--------------------------------------------
1.insert node at beginning
2.insert node at given position
3.display linked list
4.exit
enter your choice:1
inserting node to be inserted:10
element inserted at beginning
------------------------------------------
operations on single linked list
--------------------------------------------
1.insert node at beginning
2.insert node at given position
3.display linked list
4.exit
enter your choice:1
inserting node to be inserted:20
element inserted at beginning
------------------------------------------
operations on single linked list
--------------------------------------------
1.insert node at beginning
2.insert node at given position
3.display linked list
4.exit
enter your choice:1
inserting node to be inserted:30
element inserted at beginning
------------------------------------------
operations on single linked list
--------------------------------------------
1.insert node at beginning
2.insert node at given position
3.display linked list
4.exit
enter your choice:1
inserting node to be inserted:40
element inserted at beginning
------------------------------------------
operations on single linked list
--------------------------------------------
1.insert node at beginning
2.insert node at given position
3.display linked list
4.exit
enter your choice:1
inserting node to be inserted:50
element inserted at beginning
------------------------------------------
operations on single linked list
--------------------------------------------
1.insert node at beginning
2.insert node at given position
3.display linked list
4.exit
enter your choice:2
inserting node at a given position
enter the value to be inserted 35
enter the position at which node to be inserted 3
------------------------------------------
operations on single linked list
--------------------------------------------
1.insert node at beginning
2.insert node at given position
3.display linked list
4.exit
enter your choice:3
display elements of linked list
elements of list are:50->40->35->30->20->10->NULL
------------------------------------------
operations on single linked list
--------------------------------------------
1.insert node at beginning
2.insert node at given position
3.display linked list
4.exit
enter your choice:4
It's a information of data structures thanks for this information
ReplyDelete