Data Structures through C & C++ for beginners

If the code doesn't work, please replace the single quotes and double quotes(Actually these are not proper single and double quotes) in the code with single quotes and double quotes using your keyboard..

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();
}

Advertisement

18 Responses to “OPERATIONS ON SINGLE LINKED LIST”

  1. saket said

    thanks a lot!!!!!!!

  2. adiputra said

    please code in C programming…can you teach me…or please send a sample code with C programming .

  3. please code the program in c language please send me a sample code in c pogramming

  4. muzaffar said

    sooo helpful for advanced data structure……….

  5. ronnie said

    do you have any example code for C programs ?

  6. jagadeesh said

    please send the program in c using data structures

  7. ankitha raj said

    i want a program in operation on singly linked list

  8. swaathi said

    thanks a lot…… it will be very useful if u give explanation about each line

  9. Irshad said

    Its very esy programm

  10. swati said

    thanks for this but can you please give me the code for creat function?

  11. anu said

    i want singly linked list program vt diagram in easy way.imediatly plssssssss

  12. Nishinraj said

    A simple program for the same:
    http://www.programminghelp.in/2011/10/05/c-program-to-implement-linked-list-operations/

  13. Rohitha Gagarin said

    Thanq so much Mr.Vinod. Your presentation is very helpful to many of the students like us……..

  14. amruta said

    vry easy soln

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 70 other followers