Aim:
Write a C++ program to implement a Single linked list
Program:
#include<iostream.h> class Node { public: int data; Node* next; }; class List:public Node { Node *start; public: List() { start=NULL; } void create() {int num; Node *temp=new Node; if(start==NULL) { cout<<"\nEnter an Element:"; cin>>num; temp->data=num; temp->next=NULL; start=temp; } else { cout<<"single linked list has an Elements, can not create new list"; } } void insert() { Node *prev,*last; int count,pos,ch,num; Node *temp=new Node; cout<<"\nEnter an Element:"; cin>>num; temp->data=num; temp->next=NULL; cout<<"\nINSERT AS\n1:startNODE\n2:LASTNODE\n3:At specific Node"; cout<<"\nEnter Your Choice:"; cin>>ch; switch(ch) { case 1: temp->next=start; start=temp; break; case 2: last=start; while(last->next!=NULL) last=last->next; last->next=temp; break; case 3: Node *p,*n; cout<<"\nEnter the Position to Insert:"; cin>>pos; n=start; while(count!=pos) { p=n; n=n->next; count++; } if(count==pos) { p->next=temp; temp->next=n; } else cout<<"\nNot Able to Insert"; break; } } void del() { Node *prev,*last=start; int count=1,pos,ch; cout<<"\nDELETE\n1:startNODE\n2:LASTNODE\n3:At specific Node"; cout<<"\nEnter Your Choice:"; cin>>ch; switch(ch) { case 1: if(start!=NULL) { cout<<"\nDeleted Element is "<<start->data; start=start->next; } else cout<<"\nNot Able to Delete"; break; case 2: while(last->next!=NULL) { prev=last; last=last->next; } cout<<"\nDeleted Element is: "<<last->data; prev->next=NULL; break; case 3: Node *p,*n; cout<<"\nEnter the Position to Insert:"; cin>>pos; n=start; while(count!=pos) { p=n; n=n->next; count++; } if(count==pos) { cout<<"\nDeleted Element is: "<<n->data; p->next=n->next; } else cout<<"\nNot Able to Delete"; break; } } void display() { Node *last=start; if(last==NULL) { cout<<"\nList is Empty"; } while(last!=NULL) { cout<<last->data; cout<<"-->"; last=last->next; } cout<<"NULL"; } }; int main() { List l; int ch; while(1) { cout<<"\n**** MENU ****"; cout<<"\n1:CREATE FRIST NODE \n2:INSERT\n3:DELETE\n4:DISPLAY\n5:EXIT\n"; cout<<"\nEnter Your Choice:"; cin>>ch; switch(ch) { case 1: l.create(); break; case 2: l.insert(); break; case 3: l.del(); break; case 4: l.display(); break; case 5: return 0; } } return 0; }
Output:
*** MENU *** 1:CREATE FRIST NODE 2:INSERT 3:DELETE 4:DISPLAY 5:EXIT Enter your choice 1 Enter an element 10 *** MENU *** 1:CREATE FRIST NODE 2:INSERT 3:DELETE 4:DISPLAY 5:EXIT Enter your choice 2 Enter an element 20 Insert as 1:startNODE 2:LASTNODE 3:At specific Node Enter your choice 1 *** MENU *** 1:CREATE FRIST NODE 2:INSERT 3:DELETE 4:DISPLAY 5:EXIT Enter your choice 2 Enter an element 30 Insert as 1:startNODE 2:LASTNODE 3:At specific Node Enter your choice 3 Enter the position to insert 3 *** MENU *** 1:CREATE FRIST NODE 2:INSERT 3:DELETE 4:DISPLAY 5:EXIT Enter your choice 4 201030NULL *** MENU *** 1:CREATE FRIST NODE 2:INSERT 3:DELETE 4:DISPLAY 5:EXIT Enter your choice 2 Enter an element 40 Insert as 1:start node 2:last node 3:at specific node Enter your choice 2 *** MENU *** 1:CREATE FRIST NODE 2:INSERT 3:DELETE 4:DISPLAY 5:EXIT Enter your choice 3 Delete 1:start node 2:last node 3:at specific node Enter your choice 3 Enter the position to insert 2 Deleted element is 10 *** MENU *** 1:CREATE FRIST NODE 2:INSERT 3:DELETE 4:DISPLAY 5:EXIT Enter your choice 4 203040NULL *** MENU *** 1:CREATE FRIST NODE 2:INSERT 3:DELETE 4:DISPLAY 5:EXIT Enter your choice 5
Result:
Thus, the implementation of a single linked list was successfully executed.