|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | + |
| 4 | +struct node |
| 5 | +{ |
| 6 | + int data; |
| 7 | + struct node *next; |
| 8 | + struct node *prev; |
| 9 | +}; |
| 10 | + |
| 11 | +struct node *head = NULL, *last = NULL; |
| 12 | + |
| 13 | +struct node *createNode(int key) |
| 14 | +{ |
| 15 | + head = (struct node *)malloc(sizeof(struct node)); |
| 16 | + head->data = key; |
| 17 | + head->next = NULL; |
| 18 | + head->prev = NULL; |
| 19 | + last = head; |
| 20 | + return head; |
| 21 | +} |
| 22 | + |
| 23 | +struct node *additionOfNodeAtBeginning( int key) |
| 24 | +{ |
| 25 | + struct node *temp; |
| 26 | + temp = (struct node *)malloc(sizeof(struct node)); |
| 27 | + temp->data = key; |
| 28 | + temp->next = NULL; |
| 29 | + temp->next = head; |
| 30 | + temp->prev = NULL; |
| 31 | + head->prev = temp; |
| 32 | + head = temp; |
| 33 | + return head; |
| 34 | +} |
| 35 | + |
| 36 | +struct node *additionOfNodeAtEnding( int key) |
| 37 | +{ |
| 38 | + struct node *temp; |
| 39 | + temp = (struct node *)malloc(sizeof(struct node)); |
| 40 | + temp->data = key; |
| 41 | + temp->next = NULL; |
| 42 | + temp->prev = last; |
| 43 | + last->next = temp; |
| 44 | + temp->next = NULL; |
| 45 | + last = temp; |
| 46 | + return last; |
| 47 | +} |
| 48 | + |
| 49 | +struct node *additionOfNodeAtRandomeIndex( int key, int index) |
| 50 | +{ |
| 51 | + struct node *temp; |
| 52 | + temp = (struct node *)malloc(sizeof(struct node)); |
| 53 | + temp->data = key; |
| 54 | + temp->next = NULL; |
| 55 | + struct node *temp1, *temp2, previous; |
| 56 | + temp1 = head; |
| 57 | + int i = 0; |
| 58 | + while (i < index - 1 && temp1->next != NULL) |
| 59 | + { |
| 60 | + temp1 = temp1->next; |
| 61 | + i++; |
| 62 | + } |
| 63 | + if (temp1->next == NULL) |
| 64 | + { |
| 65 | + additionOfNodeAtEnding(key); |
| 66 | + } |
| 67 | + temp2 = temp1->next; |
| 68 | + temp->prev = temp1; |
| 69 | + temp1->next = temp; |
| 70 | + temp->next = temp2; |
| 71 | + temp2->prev = temp; |
| 72 | + return head; |
| 73 | +} |
| 74 | + |
| 75 | +struct node *deletingFromBeginning() |
| 76 | +{ |
| 77 | + struct node *temp; |
| 78 | + temp = head; |
| 79 | + head = head->next; |
| 80 | + head->prev = NULL; |
| 81 | + free(temp); |
| 82 | + return head; |
| 83 | +} |
| 84 | + |
| 85 | +struct node *deletingFromEnding() |
| 86 | +{ |
| 87 | + struct node *temp, *temp2; |
| 88 | + temp=last->prev; |
| 89 | + temp2=last; |
| 90 | + temp->next=NULL; |
| 91 | + last=temp; |
| 92 | + free(temp2); |
| 93 | + return head; |
| 94 | +} |
| 95 | + |
| 96 | +struct node *deletingFromRandomIndex(int index) |
| 97 | +{ |
| 98 | + struct node *temp, *temp2; |
| 99 | + temp = head; |
| 100 | + int i = 0; |
| 101 | + while (i < index - 1) |
| 102 | + { |
| 103 | + i++; |
| 104 | + temp = temp->next; |
| 105 | + } |
| 106 | + if (temp->next == NULL) |
| 107 | + { |
| 108 | + return deletingFromEnding(); |
| 109 | + } |
| 110 | + temp2 = temp->next; |
| 111 | + temp2->next->prev = temp; |
| 112 | + temp->next = temp->next->next; |
| 113 | + free(temp2); |
| 114 | + return head; |
| 115 | +} |
| 116 | + |
| 117 | +void displayInReverse() |
| 118 | +{ |
| 119 | + printf("the Linked List in Reverse Order is "); |
| 120 | + struct node *temp = last; |
| 121 | + while (temp != NULL) |
| 122 | + { |
| 123 | + printf("%d ", temp->data); |
| 124 | + temp = temp->prev; |
| 125 | + } |
| 126 | + printf("\n\n"); |
| 127 | +} |
| 128 | + |
| 129 | +void display() |
| 130 | +{ |
| 131 | + printf("the Linked List is "); |
| 132 | + struct node *temp = head; |
| 133 | + while (temp != NULL) |
| 134 | + { |
| 135 | + printf("%d ", temp->data); |
| 136 | + temp = temp->next; |
| 137 | + } |
| 138 | + printf("\n\n"); |
| 139 | +} |
| 140 | + |
| 141 | +int main() |
| 142 | +{ |
| 143 | + struct node *root = NULL; |
| 144 | + |
| 145 | + root = createNode( 4); |
| 146 | + |
| 147 | + additionOfNodeAtBeginning(6); |
| 148 | + additionOfNodeAtEnding(90); |
| 149 | + printf("After adding 90 at last ,"); |
| 150 | + display(); |
| 151 | + additionOfNodeAtRandomeIndex(12,1); |
| 152 | + printf("After adding 12 at index 1 ,"); |
| 153 | + display(); |
| 154 | + additionOfNodeAtBeginning(1); |
| 155 | + additionOfNodeAtEnding(2); |
| 156 | + additionOfNodeAtBeginning(5); |
| 157 | + additionOfNodeAtEnding(98); |
| 158 | + additionOfNodeAtRandomeIndex(122,2); |
| 159 | + display(); |
| 160 | + deletingFromBeginning(); |
| 161 | + printf("After Deletion from beginning, "); |
| 162 | + display(); |
| 163 | + deletingFromEnding(); |
| 164 | + printf("After deleting from end, "); |
| 165 | + display(); |
| 166 | + deletingFromRandomIndex(3); |
| 167 | + printf("After deleting from index 3 ,"); |
| 168 | + display(); |
| 169 | + |
| 170 | + displayInReverse(); |
| 171 | +} |
0 commit comments