NAME :- VISHWATEJ MAHESH SHENDE
ROLL NO- 36
DIV- IT C BATCH- 3
ADS assignment 7
//Write C/C++ program to implement linked list
creation, insertion of node at the front, at the end and
at the specific position. Also perform deletion of node
from the front, from the end and from specific
position. Use switch case and take user choice inputs.
#include <iostream>
Using namespace::std;
Class node{
Public:
Node* next;
Int val;
// public:
Node(int s){
Val=s;
Next=NULL;
}
};
Class linkedlist{
Public:
Node* head;
Node* tail;
Linkedlist(){
Head=tail=NULL;
}
Void pushf(int s){
If(head==NULL){
Head=tail=new node(s);
Return;
}
Node* dum=new node(s);
Dum->next=head;
Head=dum;
}
Void pushb(int p){
If(head==NULL){
Head=tail=new node(p);
Return;
}
Node* dum=new node(p);
Tail->next=dum;
Tail=dum;
}
Void print(){
Node* dum=head;
While(dum!=NULL){
Cout<<dum->val<<” “;
Dum=dum->next;
}
Cout<<endl;
}
Void pushat(int p,int vl){
Int i=2;
If(p==1){
Pushf(vl);
Return;
}
Node* dum=head;
While(i<p){
If(dum==NULL){
Cout<<”out of limit pls enter valid
pos”<<endl;
Return;
}
Dum=dum->next;
I++;
}
If(dum->next==NULL){
Pushb(vl);
Return;
}
Node* bum=new node(vl);
Bum->next=dum->next;
Dum->next=bum;
}
Void popf(){
If(head==NULL){
Cout<<”list is empty”<<endl;
Return;
}else
If(head==tail){
Delete head;
Head=tail=NULL;
Return;
}
Node* dum=head;
Head=head->next;
Delete dum;
}
Void popb(){
If(head==NULL){
Cout<<”list is empty”<<endl;
Return;
}else
If(head==tail){
Delete head;
Head=tail=NULL;
Return;
}
Node* bum=head;
While(bum->next->next!=NULL){
Bum=bum->next;
}
Bum->next=NULL;
Delete tail;
Tail=bum;
}
Void popat(int p){
Int i=2;
If(p==1){
Popf();
Return;
}
Node* dum=head;
While(i<p){
If(dum->next->next==NULL){
Cout<<”out of limit pls enter valid
pos”<<endl;
Return;
}
Dum=dum->next;
I++;
}
If(dum->next->next==NULL){
Popb();
Return;
}
Node* bum=dum->next;
Dum->next=bum->next;
Delete bum;
}
};
Int main() {
// Write C++ code here
Linkedlist s;
Int a=0,input=0,pos=0;
Cout<<”press 1 for push element at front”<<endl;
Cout<<”press 2 for push element at end”<<endl;
Cout<<”press 3 for push element at specific
location”<<endl;
Cout<<”press 4 for pop element at front”<<endl;
Cout<<”press 5 for pop element at end”<<endl;
Cout<<”press 6 for pop element at specific
location”<<endl;
Cout<<”press 7 for printing the list”<<endl;
Cout<<”press 8 for exit”<<endl;
While(a!=-1){
// cout<<”press 1 for push element at
front”<<endl;
// cout<<”press 2 for push element at end”<<endl;
// cout<<”press 3 for push element at specific
location”<<endl;
// cout<<”press 4 for pop element at front”<<endl;
// cout<<”press 5 for pop element at end”<<endl;
// cout<<”press 6 for pop element at specific
location”<<endl;
// cout<<”press 7 for printing the list”<<endl;
// cout<<”press 8 for exit”<<endl;
Cout<<”\n input-“;
Cin>>a;
Switch(a){
Case 1:
Cout<<”\n enter the value”<<endl;
Cin>>input;
s.pushf(input);
break;
case 2:
cout<<”\n enter the value”<<endl;
cin>>input;
s.pushb(input);
break;
case 3:
cout<<”\n enter the value”<<endl;
cin>>input;
cout<<”\n enter the position”<<endl;
cin>>pos;
s.pushat(pos,input);
break;
case 4:
s.popf();
break;
case 5:
s.popb();
break;
case 6:
cout<<”\n enter the position”<<endl;
cin>>pos;
s.popat(pos);
break;
case 7:
s.print();
break;
case 8:
cout<<”thank you”;
a=-1;
break;
default:
cout<<”invalid input”<<endl;
break;
}
}
Return 0;
}