Doubly Linked List :Doubly Linked List (Data Structure) In C++ . A doubly-linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes.
Doubly Linked List is a variation of Linked list in which navigation is possible in both ways, either forward and backward easily as compared to Single Linked List. Following are the important terms to understand the concept of doubly linked list.
Link − Each link of a linked list can store a data called an element.
Next − Each link of a linked list contains a link to the next link called Next.
Prev − Each link of a linked list contains a link to the previous link called Prev.
LinkedList − A Linked List contains the connection link to the first link called First and to the last link called Last.
Basic Operations
Following are the basic operations supported by a list.
Insertion − Adds an element at the beginning of the list.
Deletion − Deletes an element at the beginning of the list.
Insert Last − Adds an element at the end of the list.
Delete Last − Deletes an element from the end of the list.
Insert After − Adds an element after an item of the list.
Delete − Deletes an element from the list using the key.
Display forward − Displays the complete list in a forward manner.
Display backward − Displays the complete list in a backward manner.
Add number in Doubly Linked List
void add() { if(start==NULL) { temp=new list; cout<<"Enter Data: "; cin>>temp->data; temp->next=NULL; temp->prev=NULL; start=temp; } else{ temp=new list; cout<<"Enter Data: "; cin>>temp->data; temp->next=NULL; for(temp1=start;temp1->next!=NULL;temp1=temp1->next) {} temp1->next=temp; temp->prev=temp1; } }
Remove number in Doubly Linked List
void remove() { if(start==NULL) cout<<"Empty"<<endl; else{ if(start->next==NULL) { start=NULL; } else{ for(temp=start;temp->next!=NULL;temp=temp->next){} temp->prev->next=NULL; } } }
Update Number in Doubly Linked List
void update() {int n,x=0; if(start==NULL) cout<<"Empty"<<endl; else{ cout<<"Enter number which uh wana update: "; cin>>n; for(temp=start;temp!=NULL;temp=temp->next) {if(temp->data==n) { cout<<"Enter New Number: "; cin>>temp->data; x++; break;}}} if(x==0) cout<<"Number Not found"<<endl; }
Remove Number from a specific location in Doubly Linked List
void by_loc() {int l,x=0; if(start==NULL) cout<<"Empty"<<endl; else{ cout<<"Enter Location: "; cin>>l; if(l==1) { start=start->next; } else { temp1=start; for(int i=2;temp1!=NULL;i++) { temp=temp1; temp1=temp1->next; if(l==i) { temp->next=temp1->next; x++; break; }}}} if(x==0) cout<<"No number found"; }
Display function of Doubly Linked List
void show() { system("cls"); if(start==NULL) cout<<"Empty"<<endl; else { for(temp=start;temp!=NULL;temp=temp->next) cout<<temp->data<<endl; system("pause"); system("cls"); }}