OPERATIONS ON SINGLE LINKED LIST
Posted by Vinod on December 17, 2006
/*********************************************************
-> This C++ program is to perform the following operations
on a singly linked list
1)insertion
2)deletion
3)forward traversal
4)reverse traversal
5)search
->node structure
1) integer data
2) pointer to next node
-> This program works in microsoft VC++ environment
in windows xp
-> This program uses the following header files
1)iostream.h
********************************************************/
#include<iostream.h>
class sll
{
private:
int data;
sll *next;
public:
sll* insert_one(int,sll*);
sll* delete_one(int,sll*);
void ftraverse(sll*);
void rtraverse(sll*);
void search(int,sll*);
void function();
};
void sll::function()
{
cout<<”******************************************\n”;
cout<<”program to implement a singly linked list \n”;
cout<<”******************************************\n”;
sll * head;
head=NULL;
cout<<”\n\nMENU :\n”;
cout<<”1)insertion\n”
<<”2)deletion\n”
<<”3)forward traversal\n”
<<”4)reverse traversal\n”
<<”5)search\n”
<<”6)exit\n\n”;
cout<<”Enter your option :”;
int opt;
cin>>opt;
int d;
while(opt!=6)
{
switch(opt)
{
case 1:
cout<<”Enter data to the node :”;
cin>>d;
head=insert_one(d,head);
cout<<”inserted\n”;
break;
case 2:
cout<<”Enter the data to be deleted :”;
cin>>d;
head=delete_one(d,head);
break;
case 3:
cout<<”The forward traversal is :\n”;
ftraverse(head);
break;
case 4:
cout<<”The reverse traversal is :\n”;
rtraverse(head);
cout<<”NULL\n”;
break;
case 5:
cout<<”Enter the element to be searched :”;
int d;
cin>>d;
search(d,head);
break;
case 6:
break;
}
cout<<”\n\nMENU :\n”;
cout<<”1)insertion\n”
<<”2)deletion\n”
<<”3)forward traversal\n”
<<”4)reverse traversal\n”
<<”5)search\n”
<<”6)exit\n\n”;
cout<<”Enter your option :”;
cin>>opt;
}
}
sll* sll::insert_one(int d,sll* s)
{
sll*NEW;
NEW=new sll;
NEW->data=d;
NEW->next=NULL;
if(s==NULL)
s=NEW;
else
{
sll*s1=s;
while(s1->next!=NULL)
s1=s1->next;
s1->next=NEW;
}
return s;
}
sll* sll::delete_one(int d,sll* s)
{
if(s==NULL)
{
cout<<”list empty \n”;
return NULL;
}
if(s->data==d)
s=s->next;
else
{
sll *s1=s;
while( s1->next && s1->next->data!=d )
{
if(s1->next->data==d)
{
cout<<”deleted”;
s1->next=s1->next->next;
return s;
}
s1=s1->next;
}
cout<<”not found”;
}
return s;
}
void sll::ftraverse(sll* s)
{
sll * s1=s;
while(s1!=NULL)
{
cout<<s1->data<<” -> “;
s1=s1->next;
}
cout<<”NULL\n”;
}
void sll::rtraverse(sll* s)
{
if(s==NULL)
return;
else
rtraverse(s->next);
cout<<s->data<<”->”;
}
void sll::search(int d,sll* s)
{
while(s!=NULL)
{
if(s->data==d)
{
cout<<”found\n”;
return ;
}
s=s->next;
}
cout<<” search unsuccess ful \n”;
}
void main()
{
sll list;
list.function();
}
saket said
thanks a lot!!!!!!!
adiputra said
please code in C programming…can you teach me…or please send a sample code with C programming .
sonalbansal said
please code the program in c language please send me a sample code in c pogramming
muzaffar said
sooo helpful for advanced data structure……….
ronnie said
do you have any example code for C programs ?
jagadeesh said
please send the program in c using data structures
ankitha raj said
i want a program in operation on singly linked list
Nishinraj said
Program for singly linked list:
http://www.programminghelp.in/2011/10/05/c-program-to-implement-linked-list-operations/
swaathi said
thanks a lot…… it will be very useful if u give explanation about each line
Nishinraj said
Check out this page, program for linked list operations, it has explanation also:
http://www.programminghelp.in/2011/10/05/c-program-to-implement-linked-list-operations/
Irshad said
Its very esy programm
swati said
thanks for this but can you please give me the code for creat function?
anu said
i want singly linked list program vt diagram in easy way.imediatly plssssssss
anu said
for create,insert,delete,position,location,display algorihm for singly linked list
Nishinraj said
Check this page, this may be what you are looking for:
http://www.programminghelp.in/2011/10/05/c-program-to-implement-linked-list-operations/
Nishinraj said
A simple program for the same:
http://www.programminghelp.in/2011/10/05/c-program-to-implement-linked-list-operations/
Rohitha Gagarin said
Thanq so much Mr.Vinod. Your presentation is very helpful to many of the students like us……..
amruta said
vry easy soln