Sunday 6 October 2013

C++ code to implement single linked list

#include<iostream>

using namespace std;

#include<conio.h>

template<class T>

class node

{

     public:

              T data;

              node<T> *link;

};

template<class T>

class list

{

      private:

              node<T> *first;

      public:

      list();

      ~list();

      void createlist();

      void deletion();

      void insertion();

      void display();
      
      };

template<class T>

list<T>::list()

{

               first=NULL;

}

template<class T>

list<T>::~list()

{

                node<T> *next;

                while(first)

                {

                            next=first->link;

                            delete first;

                            first=next;

                }

}

template<class T>

void list<T>::createlist()

{

                     T a;
                     
                     node<T> *cur,*ptr;

                     cout<<"\nenter data for new node:";

                     cin>>a;

                     while(a)

                     {

                             cur=new node<T>;

                             cur->data=a;

                             cur->link=NULL;

                             if(first==NULL)

                             first=cur;

                             else

                             ptr->link=cur;

                             ptr=cur;

                             cout<<"\nenter data for new node :";

                             cin>>a;

                     }

}

template<class T>

void list<T>::insertion()

{
                node<T> *cur,*ptr;

                 T ele;
                   
                 char ch; 
          
                 ptr=first;

                 cur=new node<T>;

                 cout<<"\n enter data for new node :";

                 cin>>cur->data;

                 cur->link=NULL;

                 cout<<"\n do u want to insert first [y/n]:";

                 cin>>ch;

                 if(ch=='Y'||ch=='y')

                 {

                                     cur->link=first;

                                     first=cur;

                 }

                 else

                 {

                     cout<<"\n after which element do u want to insert :";

                     cin>>ele;

                     while(ptr!=NULL)

                     {

                                     if(ptr->data==ele)

                                     {

                                                      cur->link=ptr->link;

                                                      ptr->link=cur;

                                                      break;

                                     }

                                     else

                                     {

                                         ptr=ptr->link;

                                     }

                     }

                 }

}

template<class T>

void list<T>::deletion()

{

                 T ele;

                 char ch;

                 node<T> *ptr,*ptr1;

                 if(first==NULL)

                 {

                                cout<<"\nlist is empty .....";

                 }

                 else
                 
                 {

                     ptr=first;

                     cout<<"\ndo u want to delete first element [y/n]:";

                     cin>>ch;

                     if(ch=='y'||ch=='Y')

                     {

                                         first=first->link;

                                         delete ptr;

                     }

                     else

                     {

                         cout<<"\nwhich element do u want to delete :";

                         cin>>ele;

                         while(ptr!=NULL)

                         {

                                         if(ptr->link->data==ele)

                                         {

                                                                 ptr1=ptr->link;

                                                                 ptr->link=ptr1->link;

                                                                 delete ptr1;
                                                                 
                                                                 return;

                                         }
                                        
                                        else

                                         {

                                             ptr=ptr->link;

                                         }

                         }

                     }

                 }

}

template<class T>

void list<T>::display()

{

     node<T> *ptr;

     if(first==NULL)

     {

                    cout<<"\n list is empty..";

     }

     else

     {

         ptr=first;

         while(ptr!=NULL)

         {
                         
                         cout<<ptr->data<<"   ";

                         ptr=ptr->link;

         }

     }

}

int main()

{

    int n;

    list <int> l;

    l.createlist();

    do

    {

                   cout<<"\n 1.insertion \n2.deletion \n3.display \n4.exit \n";

                   cout<<"\n enter ur option : ";

                   cin>>n;

                   switch(n)

                   {

                            case 1: l.insertion();

                            break;

                            case 2: l.deletion();

                            break;

                            case 3: l.display();

                            break;

                            case 4:
                                 exit(0);

                            break;

                   }

    }while(n<=4);
    
    _getch();

    return 0;

}

No comments:

Post a Comment