Let us see the C++ source code of Insert at the end in Doubly Link List.
Doubly Link List – insert at the end
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->NextNode = NULL; newdoublyNode->PreviousNode = head; if(head != NULL) {head->NextNode = 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 36 |
//Insert at the end in Doubly Link List C++ - 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->NextNode = NULL; newdoublyNode->PreviousNode = head; if(head != NULL) {head->NextNode = newdoublyNode ;
} head = newdoublyNode; } void display() { struct DoublyNode* ptr; ptr = head; while(ptr != NULL) { cout<< ptr->data <<" "; ptr = ptr->PreviousNode; } } int main() { insert(4); insert(5); insert(6); cout<<"The doubly linked list is: "; display(); return 0; } |
Output
The doubly linked list is: 6 5 4
Doubly Link List – delete at the end in Doubly Link List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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->PreviousNode; head->NextNode = 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 |
//Insert and delete at the end in Doubly Link List (right most node among other nodes) C++ - 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->NextNode = NULL; newdoublyNode->PreviousNode = head; if(head != NULL) {head->NextNode = 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->PreviousNode; head->NextNode = NULL; free(temp); } void display() { struct DoublyNode* ptr; ptr = head; while(ptr != NULL) { cout<< ptr->data <<" "; ptr = ptr->PreviousNode; } } int main() { insert(4); insert(5); insert(6); del(); cout<<"The doubly linked list is: "; display(); return 0; } |