|
| 1 | + |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | + |
| 5 | +/* a node of the doubly linked list */ |
| 6 | +struct Node |
| 7 | +{ |
| 8 | + int data; |
| 9 | + struct Node *next; |
| 10 | + struct Node *prev; |
| 11 | +}; |
| 12 | + |
| 13 | +/* Function to delete a node in a Doubly Linked List. |
| 14 | + head_ref --> pointer to head node pointer. |
| 15 | + del --> pointer to node to be deleted. */ |
| 16 | +void deleteNode(struct Node **head_ref, struct Node *del) |
| 17 | +{ |
| 18 | + /* base case */ |
| 19 | + if(*head_ref == NULL || del == NULL) |
| 20 | + return; |
| 21 | + |
| 22 | + /* If node to be deleted is head node */ |
| 23 | + if(*head_ref == del) |
| 24 | + *head_ref = del->next; |
| 25 | + |
| 26 | + /* Change next only if node to be deleted is NOT the last node */ |
| 27 | + if(del->next != NULL) |
| 28 | + del->next->prev = del->prev; |
| 29 | + |
| 30 | + /* Change prev only if node to be deleted is NOT the first node */ |
| 31 | + if(del->prev != NULL) |
| 32 | + del->prev->next = del->next; |
| 33 | + |
| 34 | + /* Finally, free the memory occupied by del*/ |
| 35 | + free(del); |
| 36 | + return; |
| 37 | +} |
| 38 | + |
| 39 | +/* UTILITY FUNCTIONS */ |
| 40 | +/* Function to insert a node at the beginning of the Doubly Linked List */ |
| 41 | +void push(struct Node** head_ref, int new_data) |
| 42 | +{ |
| 43 | + /* allocate node */ |
| 44 | + struct Node* new_node = |
| 45 | + (struct Node*) malloc(sizeof(struct Node)); |
| 46 | + |
| 47 | + /* put in the data */ |
| 48 | + new_node->data = new_data; |
| 49 | + |
| 50 | + /* since we are adding at the begining, |
| 51 | + prev is always NULL */ |
| 52 | + new_node->prev = NULL; |
| 53 | + |
| 54 | + /* link the old list off the new node */ |
| 55 | + new_node->next = (*head_ref); |
| 56 | + |
| 57 | + /* change prev of head node to new node */ |
| 58 | + if((*head_ref) != NULL) |
| 59 | + (*head_ref)->prev = new_node ; |
| 60 | + |
| 61 | + /* move the head to point to the new node */ |
| 62 | + (*head_ref) = new_node; |
| 63 | +} |
| 64 | + |
| 65 | +/* Function to print nodes in a given doubly linked list |
| 66 | + This function is same as printList() of singly linked lsit */ |
| 67 | +void printList(struct Node *node) |
| 68 | +{ |
| 69 | + while(node!=NULL) |
| 70 | + { |
| 71 | + printf("%d ", node->data); |
| 72 | + node = node->next; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/* Drier program to test above functions*/ |
| 77 | +int main() |
| 78 | +{ |
| 79 | + /* Start with the empty list */ |
| 80 | + struct Node* head = NULL; |
| 81 | + |
| 82 | + /* Let us create the doubly linked list 10<->8<->4<->2 */ |
| 83 | + push(&head, 2); |
| 84 | + push(&head, 4); |
| 85 | + push(&head, 8); |
| 86 | + push(&head, 10); |
| 87 | + |
| 88 | + printf("\n Original Linked list "); |
| 89 | + printList(head); |
| 90 | + |
| 91 | + /* delete nodes from the doubly linked list */ |
| 92 | + deleteNode(&head, head); /*delete first node*/ |
| 93 | + deleteNode(&head, head->next); /*delete middle node*/ |
| 94 | + deleteNode(&head, head->next); /*delete last node*/ |
| 95 | + |
| 96 | + /* Modified linked list will be NULL<-8->NULL */ |
| 97 | + printf("\n Modified Linked list "); |
| 98 | + printList(head); |
| 99 | + |
| 100 | + getchar(); |
| 101 | +} |
0 commit comments