OPERATIONS ON SINGLE LINKED LIST


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

About these ads

25 thoughts on “OPERATIONS ON SINGLE LINKED LIST

    • I was trying to make an app for my website using web view. Am almost done, but the problem is when I try to download songs, the music player opens and starts playing. I want it to download files rather than playing. I can’t change the file streaming settings on the server side, I must make the app to download files by itself, is that possible?? thanks in advance.

      • /* Write a program to construct a single linked list using
        dynamic variable, list should contain the student information */

        #include
        #include
        #include
        struct node
        {
        int rno,m1,m2;
        char name[10];
        struct node *link;
        };
        typedef struct node NODE;
        void main()
        {
        NODE *head,*first,*t;
        int i,n;
        float total,avg;
        clrscr();
        first=0;
        printf(“*******OUTPUT******\n”);
        printf(“Enter the number of student\n”);
        scanf(“%d”,&n);
        printf(“Enter the information of %d students\n”,n);
        for(i=0;irno,&head->name,&head->m1,&head->m2);
        if(first!=0)
        {
        t->link=head;
        t=head;
        }

        else
        {
        t=first=head;
        }
        }
        t->link=0;
        t=first;
        printf(“_____________________________________________\n”);
        printf(“The students of linked list \n”);
        printf(“_____________________________________________\n”);
        printf(“rno\tname\tm1\tm2\ttotal\tavg\n”);
        printf(“_____________________________________________\n”);
        while(t!=0)
        {
        total=(t->m1+t->m2);
        avg=total/2;
        printf(“%d\t%s\t%d\t%d\t%3.2f\t%3.2f\n”,
        t->rno,t->name,t->m1,t->m2,total,avg);
        t=t->link;
        }
        printf(“_______________________________________________\n”);
        getch();
        }

        *******OUTPUT******
        Enter the number of student
        2
        Enter the information of 2 students
        1
        Akash
        77
        88
        2
        Ashish
        88
        80
        _____________________________________________
        The students of linked list
        _____________________________________________
        rno name m1 m2 total avg
        _____________________________________________
        1 Akash 77 88 165.00 82.50
        2 Ashish 88 80 168.00 84.00
        _______________________________________________

  1. plz can u solve the question given below
    Develop and implement an algorithm that delete from list l1 nodes whose positions are to be found in an ordered list l2 for example if l1=(A->B->C->D->E) and l2=(2->4->8) the the second and fourth nodes are to be deleted from list l1 and after deletion l1=(A->C->E).

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 )

Google+ photo

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

Connecting to %s