|
| 1 | +// A complete working C program to demonstrate all insertion methods |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | + |
| 5 | +// A linked list node |
| 6 | +struct Node { |
| 7 | + int data; |
| 8 | + struct Node* next; |
| 9 | + struct Node* prev; |
| 10 | +}; |
| 11 | + |
| 12 | +/* Given a reference (pointer to pointer) to the head of a list |
| 13 | + and an int, inserts a new node on the front of the list. */ |
| 14 | +void push(struct Node** head_ref, int new_data) |
| 15 | +{ |
| 16 | + /* 1. allocate node */ |
| 17 | + struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); |
| 18 | + |
| 19 | + /* 2. put in the data */ |
| 20 | + new_node->data = new_data; |
| 21 | + |
| 22 | + /* 3. Make next of new node as head and previous as NULL */ |
| 23 | + new_node->next = (*head_ref); |
| 24 | + new_node->prev = NULL; |
| 25 | + |
| 26 | + /* 4. change prev of head node to new node */ |
| 27 | + if ((*head_ref) != NULL) |
| 28 | + (*head_ref)->prev = new_node; |
| 29 | + |
| 30 | + /* 5. move the head to point to the new node */ |
| 31 | + (*head_ref) = new_node; |
| 32 | +} |
| 33 | + |
| 34 | +/* Given a node as prev_node, insert a new node after the given node */ |
| 35 | +void insertAfter(struct Node* prev_node, int new_data) |
| 36 | +{ |
| 37 | + /*1. check if the given prev_node is NULL */ |
| 38 | + if (prev_node == NULL) { |
| 39 | + printf("the given previous node cannot be NULL"); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + /* 2. allocate new node */ |
| 44 | + struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); |
| 45 | + |
| 46 | + /* 3. put in the data */ |
| 47 | + new_node->data = new_data; |
| 48 | + |
| 49 | + /* 4. Make next of new node as next of prev_node */ |
| 50 | + new_node->next = prev_node->next; |
| 51 | + |
| 52 | + /* 5. Make the next of prev_node as new_node */ |
| 53 | + prev_node->next = new_node; |
| 54 | + |
| 55 | + /* 6. Make prev_node as previous of new_node */ |
| 56 | + new_node->prev = prev_node; |
| 57 | + |
| 58 | + /* 7. Change previous of new_node's next node */ |
| 59 | + if (new_node->next != NULL) |
| 60 | + new_node->next->prev = new_node; |
| 61 | +} |
| 62 | + |
| 63 | +/* Given a reference (pointer to pointer) to the head |
| 64 | + of a DLL and an int, appends a new node at the end */ |
| 65 | +void append(struct Node** head_ref, int new_data) |
| 66 | +{ |
| 67 | + /* 1. allocate node */ |
| 68 | + struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); |
| 69 | + |
| 70 | + struct Node* last = *head_ref; /* used in step 5*/ |
| 71 | + |
| 72 | + /* 2. put in the data */ |
| 73 | + new_node->data = new_data; |
| 74 | + |
| 75 | + /* 3. This new node is going to be the last node, so |
| 76 | + make next of it as NULL*/ |
| 77 | + new_node->next = NULL; |
| 78 | + |
| 79 | + /* 4. If the Linked List is empty, then make the new |
| 80 | + node as head */ |
| 81 | + if (*head_ref == NULL) { |
| 82 | + new_node->prev = NULL; |
| 83 | + *head_ref = new_node; |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + /* 5. Else traverse till the last node */ |
| 88 | + while (last->next != NULL) |
| 89 | + last = last->next; |
| 90 | + |
| 91 | + /* 6. Change the next of last node */ |
| 92 | + last->next = new_node; |
| 93 | + |
| 94 | + /* 7. Make last node as previous of new node */ |
| 95 | + new_node->prev = last; |
| 96 | + |
| 97 | + return; |
| 98 | +} |
| 99 | + |
| 100 | +// This function prints contents of linked list starting from the given node |
| 101 | +void printList(struct Node* node) |
| 102 | +{ |
| 103 | + struct Node* last; |
| 104 | + printf("\nTraversal in forward direction \n"); |
| 105 | + while (node != NULL) { |
| 106 | + printf(" %d ", node->data); |
| 107 | + last = node; |
| 108 | + node = node->next; |
| 109 | + } |
| 110 | + |
| 111 | + printf("\nTraversal in reverse direction \n"); |
| 112 | + while (last != NULL) { |
| 113 | + printf(" %d ", last->data); |
| 114 | + last = last->prev; |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +/* Drier program to test above functions*/ |
| 119 | +int main() |
| 120 | +{ |
| 121 | + /* Start with the empty list */ |
| 122 | + struct Node* head = NULL; |
| 123 | + |
| 124 | + // Insert 6. So linked list becomes 6->NULL |
| 125 | + append(&head, 6); |
| 126 | + |
| 127 | + // Insert 7 at the beginning. So linked list becomes 7->6->NULL |
| 128 | + push(&head, 7); |
| 129 | + |
| 130 | + // Insert 1 at the beginning. So linked list becomes 1->7->6->NULL |
| 131 | + push(&head, 1); |
| 132 | + |
| 133 | + // Insert 4 at the end. So linked list becomes 1->7->6->4->NULL |
| 134 | + append(&head, 4); |
| 135 | + |
| 136 | + // Insert 8, after 7. So linked list becomes 1->7->8->6->4->NULL |
| 137 | + insertAfter(head->next, 8); |
| 138 | + |
| 139 | + printf("Created DLL is: "); |
| 140 | + printList(head); |
| 141 | + |
| 142 | + getchar(); |
| 143 | + return 0; |
| 144 | +} |
0 commit comments