#include #include #include struct Node { int data; Node *next; }; Node *start=NULL; void menu() { cout<<"\n1. Add Element At Last"; cout<<"\n2. Add Element At First"; cout<<"\n3. Delete Element By Value"; cout<<"\n4. Display List"; cout<<"\n5. Insert Element By Location"; cout<<"\n9. Exit"; } Node * createNode() { Node *t=new Node; cout<<"\nEnter value for new node"; cin>>t->data; t->next=NULL; return(t); } void addElementLast() { Node *temp= createNode(); if(start==NULL) { start=temp; } else { Node *t=start; while(t->next!=NULL) { t=t->next; } t->next=temp; } } void addElementFirst() { Node *temp=createNode(); temp->next=start; start=temp; } void displayList() { Node *t=start; cout<<"\n"; while(t!=NULL) { cout<data<<" "; t=t->next; } } void insertElementByLocation() { int l; cout<<"\nenter location"; cin>>l; Node *t=createNode(); Node *t1,*t2; t1=start; t2=start->next; for(int i=1;i<=l-2;i++) { t1=t1->next; t2=t2->next; } t1->next=t; t->next=t2; } void deleteElementByValue() { int n; cout<<"\nEnter value to be delete"; cin>>n; if(start->data==n) { start=start->next; } else { Node *t=start; int flag=0; while(t->next->data!=n) { if(t->next==NULL) { cout<<"\nNot Found"; flag=1; break; } t=t->next; } if(flag==0) t->next=t->next->next; } } void main() { int ch; clrscr(); while(1) { menu(); cout<<"\nEnter your choice"; cin>>ch; switch(ch) { case 1: addElementLast(); break; case 2: addElementFirst(); break; case 3: deleteElementByValue(); break; case 4: displayList(); break; case 5: insertElementByLocation(); break; case 9: exit(0); } } getch(); }