|
| 1 | +#include <stdlib.h> |
| 2 | +#include <stdio.h> |
| 3 | + |
| 4 | +struct Deque |
| 5 | +{ |
| 6 | + int data; |
| 7 | + struct Deque *next; |
| 8 | +}; |
| 9 | +struct Deque *head = NULL; |
| 10 | +struct Deque *last = NULL; |
| 11 | + |
| 12 | +struct Deque *createDeque(struct Deque *root, int key) //Function to create deque |
| 13 | +{ |
| 14 | + root = (struct Deque *)malloc(sizeof(struct Deque)); |
| 15 | + root->data = key; |
| 16 | + root->next = NULL; |
| 17 | + head = root; |
| 18 | + last = root; |
| 19 | + return head; |
| 20 | +} //O(1) time complexity |
| 21 | + |
| 22 | +struct Deque *push_front(struct Deque *root, int key) //Function to push element on the front of deque |
| 23 | +{ |
| 24 | + if (head == NULL) |
| 25 | + { |
| 26 | + return createDeque(root, key); |
| 27 | + } |
| 28 | + struct Deque *temp; |
| 29 | + temp = (struct Deque *)malloc(sizeof(struct Deque)); |
| 30 | + temp->data = key; |
| 31 | + temp->next = head; |
| 32 | + head = temp; |
| 33 | + return head; |
| 34 | +} //O(1) time complexity |
| 35 | + |
| 36 | +struct Deque *push_back(struct Deque *root, int key) //Function to push element to the back of deque |
| 37 | +{ |
| 38 | + if (head == NULL) |
| 39 | + { |
| 40 | + return createDeque(root, key); |
| 41 | + } |
| 42 | + struct Deque *temp; |
| 43 | + temp = (struct Deque *)malloc(sizeof(struct Deque)); |
| 44 | + temp->data = key; |
| 45 | + temp->next = NULL; |
| 46 | + last->next = temp; |
| 47 | + last = temp; |
| 48 | + return last; |
| 49 | +} //O(1) time complexity |
| 50 | + |
| 51 | +struct Deque *pop_front() //Function to remove or pop the front element |
| 52 | +{ |
| 53 | + if (head == NULL) |
| 54 | + { |
| 55 | + printf("Deque is empty.\n"); |
| 56 | + return head; |
| 57 | + } |
| 58 | + struct Deque *temp; |
| 59 | + temp = head; |
| 60 | + head = head->next; |
| 61 | + free(temp); //free the unwanted space |
| 62 | + return head; |
| 63 | +} //O(1) time complexity |
| 64 | + |
| 65 | +struct Deque *pop_back() //Function to remove element from back of deque |
| 66 | +{ |
| 67 | + if (head == NULL) //before removing element, checking whether the deque is empty or not |
| 68 | + { |
| 69 | + printf("Deque is empty\n"); |
| 70 | + return head; |
| 71 | + } |
| 72 | + struct Deque *temp, *temp2; |
| 73 | + temp = head; |
| 74 | + while (temp->next != last) |
| 75 | + { |
| 76 | + temp = temp->next; |
| 77 | + } |
| 78 | + temp2 = temp->next; |
| 79 | + temp->next = NULL; |
| 80 | + last = temp; |
| 81 | + free(temp2);//free the unwanted space |
| 82 | + return head; |
| 83 | +} //O(n) time complexity |
| 84 | + |
| 85 | +void front() //Function to give the front element of deque |
| 86 | +{ |
| 87 | + if (head == NULL) |
| 88 | + { |
| 89 | + printf("Deque is empty.\n\n"); |
| 90 | + } |
| 91 | + printf("The element at the front of deque is %d.\n\n", head->data); |
| 92 | +} //O(1) time complexity |
| 93 | + |
| 94 | +void back() //Function to give the back of deque |
| 95 | +{ |
| 96 | + if (head == NULL) |
| 97 | + { |
| 98 | + printf("Deque is empty\n"); |
| 99 | + } |
| 100 | + printf("The element at the last of deque is %d.\n\n", last->data); |
| 101 | +} //O(1) time complexity |
| 102 | + |
| 103 | +void isEmpty() //Function to know whether deque is empty or not |
| 104 | +{ |
| 105 | + if (head == NULL) |
| 106 | + { |
| 107 | + printf("List is empty.\n\n"); |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + printf("No, list is not empty.\n\n"); |
| 112 | + } |
| 113 | +} //O(1) time complexity |
| 114 | + |
| 115 | +void displayTheDeque() //Function to print the whole deque |
| 116 | +{ |
| 117 | + if (head == NULL) |
| 118 | + { |
| 119 | + printf("Deque is empty.\n\n"); |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + printf("The Deque is \n\t "); |
| 124 | + printf("Front-----> "); |
| 125 | + struct Deque *temp = head; //initializing the temporary pointer to head of deque |
| 126 | + while (temp != NULL) |
| 127 | + { |
| 128 | + printf("%d \t", temp->data); |
| 129 | + temp = temp->next; |
| 130 | + } |
| 131 | + printf(" <-----End\n\n"); |
| 132 | + } |
| 133 | +} //O(n) time complexity |
| 134 | + |
| 135 | +int main() |
| 136 | +{ |
| 137 | + struct Deque *root = NULL; |
| 138 | + root = createDeque(root, 4); //creating Deque |
| 139 | + push_front(root, 6); //pushing data on the front |
| 140 | + push_back(root, 5); //pushing data on the back |
| 141 | + push_front(root, 61); |
| 142 | + push_front(root, 6); |
| 143 | + push_front(root, 12); |
| 144 | + push_front(root, 60); |
| 145 | + push_back(root, 68); |
| 146 | + displayTheDeque(); //Displaying the deque |
| 147 | + pop_front(); //removing front element |
| 148 | + printf("After removing from front, "); |
| 149 | + displayTheDeque(); |
| 150 | + pop_back(); //removing back element |
| 151 | + printf("After removing from back, "); |
| 152 | + displayTheDeque(); |
| 153 | + front(); //Getting value of front element of deque |
| 154 | + back(); //Getting value of back element of deque |
| 155 | + isEmpty(); //Checking if deque is empty or not |
| 156 | +} |
0 commit comments