Sunday, September 22, 2013

Shortest Program to perform operation of Singly Linked List.


#include <iostream>
#include <list>
#include<stdlib.h>
using namespace std;
int main ()
{
  std::list<int> l;
  std::list<int>::iterator it;
  do
    {int c,d,n,pos;
    cout<<"\n\t\t Single Linked List Operations. \n 1.Insert \t 2.Delete \t 3.Display \t4.Exit.\n\n Enter Your Choice:";cin>>c;
    switch(c)
    {case 1: cout<<"\n\t Insert Operations\n\t 1.Begining \t 2.End \t 3.At a given position\nEnter your choice:";cin>>d;
    switch(d)
           {case 1:cout<<"\n Enter Element::";cin>>n;l.push_front(n);break;case 2:cout<<"\n Enter Element::";cin>>n;l.push_back(n);break;
            case 3:cout<<"\n Enter Element::";cin>>n;cout<<"\n Enter Position:";cin>>pos;it = l.begin();for(int i=0;i<pos-1;i++)++it;l.insert(it,n);break;
            default:cout<<"\n Wrong Choice.!!!";break;
           }break;
case 2:if(l.size()==0){cout<<"\nEmpty List.";break;}else{cout<<"\n Enter Position::";cin>>pos;it = l.begin();for(int i=0;i<pos-1;i++)++it;l.erase(it);break;}
case 3:if(l.size()==0){cout<<"\nEmpty List.";break;}else{for (it=l.begin(); it!=l.end(); ++it)std::cout << ' ' << *it<<"\n";break;}
case 4:exit(1);break;
default:cout<<"Wrong Choice!!!!\n";break;}
}while(1);
return 0;
}

No comments:

Post a Comment