Doubly Link List C++ Easy Code
Let us see the C++ Code to insert at the start(beginning) in Doubly Link List
Doubly Link List – insert at the start(beginning)
insert at start
1 2 3 4 5 6 7 8 9 10 11 | struct DoublyNode* head = NULL; void insert(int newdata) { struct DoublyNode* newdoublyNode = (struct DoublyNode*) malloc(sizeof(struct DoublyNode)); newdoublyNode->data = newdata; newdoublyNode->PreviousNode = NULL; newdoublyNode->NextNode = head; if(head != NULL) {head->PreviousNode = newdoublyNode ; } head = newdoublyNode; } |
Complete source code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | // Doubly Link List - insert at the start(beginning)by t4tutorials.com #include <iostream> #include <cstdlib> using namespace std; struct DoublyNode { int data; struct DoublyNode *PreviousNode; struct DoublyNode *NextNode; }; struct DoublyNode* head = NULL; void insert(int newdata) { struct DoublyNode* newdoublyNode = (struct DoublyNode*) malloc(sizeof(struct DoublyNode)); newdoublyNode->data = newdata; newdoublyNode->PreviousNode = NULL; newdoublyNode->NextNode = head; if(head != NULL) {head->PreviousNode = newdoublyNode ; } head = newdoublyNode; } void display() { struct DoublyNode* ptr; ptr = head; while(ptr != NULL) { cout<< ptr->data <<" "; ptr = ptr->NextNode; } } int main() { insert(7); insert(8); cout<<"The doubly linked list is: "; display(); return 0; } |
Output
The doubly linked list is: 8 7
Doubly Link List – delete a node at the start(beginning)- (left most node)
This will delete the node on the left side of the link list. left side means at the beginning of the link list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | void del() { struct DoublyNode* temp; if(head==NULL) {cout<<"empty"; } else { temp = head; if(head->NextNode==NULL && head->PreviousNode == NULL) { free(temp); free(head); } } head = head->NextNode; head->PreviousNode = NULL; free(temp); } |
Complete source code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | // Doubly Link List - insert at the start(beginning) of the double link list by t4tutorials.com #include <iostream> #include <cstdlib> using namespace std; struct DoublyNode { int data; struct DoublyNode *PreviousNode; struct DoublyNode *NextNode; }; struct DoublyNode* head = NULL; void insert(int newdata) { struct DoublyNode* newdoublyNode = (struct DoublyNode*) malloc(sizeof(struct DoublyNode)); newdoublyNode->data = newdata; newdoublyNode->PreviousNode = NULL; newdoublyNode->NextNode = head; if(head != NULL) {head->PreviousNode = newdoublyNode ; } head = newdoublyNode; } void del() { struct DoublyNode* temp; if(head==NULL) {cout<<"empty"; } else { temp = head; if(head->NextNode==NULL && head->PreviousNode == NULL) { free(temp); free(head); } } head = head->NextNode; head->PreviousNode = NULL; free(temp); } void display() { struct DoublyNode* ptr; ptr = head; while(ptr != NULL) { cout<< ptr->data <<" "; ptr = ptr->NextNode; } } int main() { insert(7); cout<<"The doubly linked list is: "; del(); display(); return 0; } |
Output
The doubly linked list is: 7
Another code sample for delete at the beginning
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | void node_deletion() { if (head==NULL) //Supposing that no nodes have been inserted { cout<<"No node to delete"<<endl; } else { DoublyNode* temp=head; cout<<head->data<<" is deleted from the list\n"; head=head->NextNode; if (head!=NULL) { head->PreviousNode=NULL; } free(temp); } } |