© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu
Quick Links
Easy Studies


CPP Program To Implement Single Linked List:

/* single linked list */ #include #include #include class list { struct node { int data; node *link; }*p; public: void inslast(int); void insbeg(int); void insnext(int,int); void delelement(int); void delbeg(); void dellast(); void disp(); int seek(int); list(){p=NULL;} ~list(); }; void list::inslast(int x) { node *q,*t; if(p==NULL) { p=new node; p->data=x; p->link=NULL; } else { q=p; while(q->link!=NULL) q=q->link; t=new node; t->data=x; t->link=NULL; q->link=t; } cout<<"\n\nInserted successfully at the end.."; disp(); } void list:: insbeg(int x) { node *q; q=p; p=new node; p->data=x; p->link=q; cout<<"\n\nInserted successfully at the begining.."; disp(); } void list::delelement(int x) { node *q,*r; q=p; if(q->data==x) { p=q->link; delete q; return; } r=q; while(q!=NULL) { if(q->data==x) { r->link=q->link; delete q; return; } r=q; q=q->link; } cout<<"\n\nElement you entered "<link; delete q; return; } void list:: dellast() { cout<<"\n\nThe list before deletion:"; disp(); node *q,*t; q=p; if(q==NULL) { cout<<"\n\nThere is no data in the list.."; return; } if(q->link==NULL) { p=q->link; delete q; return; } while(q->link->link!=NULL) q=q->link; q->link=NULL; return; } list::~list() { node *q; if(p==NULL) return; while(p!=NULL) { q=p->link; delete p; p=q; } } void list::disp() { node *q; q=p; if(q==NULL) { cout<<"\n\nNo data is in the list.."; return; } cout<<"\n\nThe items present in the list are \n"; while(q!=NULL) { cout<data<<"\n"; q=q->link; } } void list :: insnext(int value,int position) { node *temp,*temp1; temp=p; if(temp1==NULL) { temp1= new node; temp1->data=value; temp1->link=NULL; p=temp1; return; } for(int i=0;((ilink!=NULL)) ;i++) { if(i==(position-1)) { temp1= new node; temp1->data= value; temp1->link=temp->link; temp->link=temp1; } temp=temp->link; } cout<<"\n\nInserted successfully at "<data==value) return position+1; else { temp=temp->link; position=position+1; } } cout<<"\n\nElement "<>ch; switch(ch) { case 1: clrscr(); cout<<"INSERTION"; cout<<"\n\n1.Insertion at begining\n2.Insertion at the end"; cout<<"\n3.Insertion between two Nodes"; cout<<"\n\nEnter ur choice:"; cin>>ps; cout<<"Enter the value to insert:"; cin>>v; switch(ps) { case 1: l.insbeg(v); break; case 2: l.inslast(v); break; case 3: cout<<"\nEnter the position to insert the value:"; cin>>p; l.insnext(v,p); break; default: cout<<"\nThe choice is invalid"; return; } break; case 2: clrscr(); cout<<"\n1.Delete the first element\n2.Delete the last element"; cout<<"\n3.Enter the element to delete from the list"; cout<<"\n\nEnter ur choice:"; cin>>ps; switch(ps) { case 1: l.delbeg(); cout<<"\nThe list after deletion:"; l.disp(); break; case 2: l.dellast(); cout<<"\nThe list after deletion:"; l.disp(); break; case 3: l.disp(); cout<<"\nEnter the element to delete : "; cin>>v; l.delelement(v); cout<<"\nThe list after deletion:"; l.disp(); break; default: cout<<"\nThe option is invalid..."; break; } break; case 3: clrscr(); l.disp(); break; case 4: clrscr(); l.disp(); cout<<"\nEnter the element to search:"; cin>>v; cout<<"\nThe position of the element "<< v<<" is "<

SAMPLE INPUT AND OUTPUT:

  SINGLY LINKED LIST

1.CREATE
2.INSERT
3.DELETE
4.EXIT

ENTER YOUR CHOICE : 1

ENTER THE DATA: 10

 10

1.CREATE
2.INSERT
3.DELETE
4.EXIT

ENTER YOUR CHOICE : 2

ENTER THE DATA: 30

ENTER THE POSITION: 1

 30
 10

1.CREATE
2.INSERT
3.DELETE
4.EXIT
 
ENTER YOUR CHOICE : 3

ENTER THE POSITION : 2

LIST IS EMPTY

 
SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑
bottom