Programs include Insertion and deletion function of Doubly Link List
#include<iostream>
using namespace std;
struct node{
int data;
node *next;
node *prev;
};
void insert_head(node **pointerToStart, int n){
cout<<"c 1";
node *temp=new node;
temp->data=n;
if(*pointerToStart==NULL){
temp->next=NULL;
temp->prev=NULL;
*pointerToStart=temp;
return; // agar mein if vali contion hata huin to galt ata hai ans
}
temp->next=*pointerToStart;
temp->prev=NULL;
cout<<"c 2";
(*pointerToStart)->prev=temp;
cout<<"c 3";
*pointerToStart=temp;
cout<<"c 4";
cout<<"c 5";
}
void insert_tail(node **pointerToStart, int n){
node *temp=new node;
temp->data=n;
node *t=*pointerToStart;
if(*pointerToStart==NULL){
temp->next=NULL;
temp->prev=NULL;
*pointerToStart=temp;
return;
}
while(t->next!=NULL){
t=t->next;
}
t->next=temp;
temp->prev=t;
temp->next=NULL;
}
void print_list(node** pointerToStart){
node* temp=*pointerToStart;
while(temp!=NULL){
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"NULL";
}
void del(node **pointerToStart, int n){
node *temp=*pointerToStart;
while(temp!=NULL){
if(temp->data==n){
if(temp->next==NULL&&temp->prev==NULL){
delete *pointerToStart;
*pointerToStart=NULL;
cout<<"\nNo deleted \n";
return;
}
else if(temp->next==NULL){
temp->prev->next=NULL;
cout<<"\nNo deleted \n";
return;
}
else if(temp->prev==NULL){
*pointerToStart=temp->next;
temp->next->prev=NULL;
cout<<"\nNo deleted \n";
return;
}
(temp->prev)->next=temp->next;
(temp->next)->prev=temp->prev;
delete temp;
cout<<"\nNo deleted \n";
return;
}
temp=temp->next;
}
cout<<"\nno not found";
return;
}
main(){
int d;
int t=1;
node *start=NULL,*tail=NULL;
while(t=1){
//cout<<"\n";
cout<<"\nPress 1 to add at head";
cout<<"\nPress 2 to add at tail";
//cout<<"\nPress 3 to search a no";
cout<<"\nPress 4 to print the list";
cout<<"\nPress 5 to delete";
cout<<"\nPress 6 to Buble Sort";
cout<<"\nPress 12 to exit\n";
int c;
cin>>c;
switch(c){
case 1 :
cout<<"enter data ";
cin >> d;
insert_head(&start,d);
break;
case 2 :
cout<<"enter data ";
cin >> d;
insert_tail(&start,d);
break;
case 3 :
cout<<"enter Number which you wana find ";
cin >> d;
break;
case 4 :
print_list(&start);
break;
case 5 :
cout<<"\nenter the no you wana delete ";
cin>>d;
del(&start,d);
break;
case 6:
// bubleSort(&start);
print_list(&start);
case 12 :
t=0;
}
}
}
No comments:
Post a Comment