|
3 | 3 |
|
4 | 4 | struct Deque
|
5 | 5 | {
|
6 |
| - int data; |
7 |
| - struct Deque *next; |
| 6 | + int data; |
| 7 | + struct Deque *next; |
8 | 8 | };
|
9 | 9 | struct Deque *head = NULL;
|
10 | 10 | struct Deque *last = NULL;
|
11 | 11 |
|
12 |
| -struct Deque *createDeque(struct Deque *root, int key) //Function to create deque |
| 12 | +struct Deque *createDeque(struct Deque *root, int key) //Function to create deque |
13 | 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; |
| 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 | 20 | }
|
21 | 21 |
|
22 |
| -struct Deque *push_front(struct Deque *root, int key) //Function to push element on the front of deque |
| 22 | +struct Deque *push_front(struct Deque *root, int key) //Function to push element on the front of deque |
23 | 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; |
| 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 | 34 | }
|
35 | 35 |
|
36 |
| -struct Deque *push_back(struct Deque *root, int key) //Function to push element to the back of deque |
| 36 | +struct Deque *push_back(struct Deque *root, int key) //Function to push element to the back of deque |
37 | 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; |
| 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 | 49 | }
|
50 | 50 |
|
51 |
| -struct Deque *pop_front() //Function to remove or pop the front element |
| 51 | +struct Deque *pop_front() //Function to remove or pop the front element |
52 | 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; |
| 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 | 63 | }
|
64 | 64 |
|
65 |
| -struct Deque *pop_back() //Function to remove element from back of deque |
| 65 | +struct Deque *pop_back() //Function to remove element from back of deque |
66 | 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; |
| 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 | 83 | }
|
84 | 84 |
|
85 |
| -void front() //Function to give the front element of deque |
| 85 | +void front() //Function to give the front element of deque |
86 | 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); |
| 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 | 92 | }
|
93 | 93 |
|
94 |
| -void back() //Function to give the back of deque |
| 94 | +void back() //Function to give the back of deque |
95 | 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); |
| 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 | 101 | }
|
102 | 102 |
|
103 |
| -void isEmpty() //Function to know whether deque is empty or not |
| 103 | +void isEmpty() //Function to know whether deque is empty or not |
104 | 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 |
| - } |
| 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 | 113 | }
|
114 | 114 |
|
115 |
| -void displayTheDeque() //Function to print the whole deque |
| 115 | +void displayTheDeque() //Function to print the whole deque |
116 | 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 |
| - } |
| 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 | 133 | }
|
134 | 134 | int main()
|
135 | 135 | {
|
136 |
| - struct Deque *root = NULL; |
137 |
| - root = createDeque(root, 4); //creating Deque |
138 |
| - push_front(root, 6); //pushing data on the front |
139 |
| - push_back(root, 5); //pushing data on the back |
140 |
| - push_front(root, 61); |
141 |
| - push_front(root, 6); |
142 |
| - push_front(root, 12); |
143 |
| - push_front(root, 60); |
144 |
| - push_back(root, 68); |
145 |
| - displayTheDeque(); //Displaying the deque |
146 |
| - pop_front(); //removing front element |
147 |
| - printf("After removing from front, "); |
148 |
| - displayTheDeque(); |
149 |
| - pop_back(); //removing back element |
150 |
| - printf("After removing from back, "); |
151 |
| - displayTheDeque(); |
152 |
| - front(); //Getting value of front element of deque |
153 |
| - back(); //Getting value of back element of deque |
154 |
| - isEmpty(); //Checking if deque is empty or not |
| 136 | + struct Deque *root = NULL; |
| 137 | + root = createDeque(root, 4); //creating Deque |
| 138 | + push_front(root, 6); //pushing data on the front |
| 139 | + push_back(root, 5); //pushing data on the back |
| 140 | + push_front(root, 61); |
| 141 | + push_front(root, 6); |
| 142 | + push_front(root, 12); |
| 143 | + push_front(root, 60); |
| 144 | + push_back(root, 68); |
| 145 | + displayTheDeque(); //Displaying the deque |
| 146 | + pop_front(); //removing front element |
| 147 | + printf("After removing from front, "); |
| 148 | + displayTheDeque(); |
| 149 | + pop_back(); //removing back element |
| 150 | + printf("After removing from back, "); |
| 151 | + displayTheDeque(); |
| 152 | + front(); //Getting value of front element of deque |
| 153 | + back(); //Getting value of back element of deque |
| 154 | + isEmpty(); //Checking if deque is empty or not |
155 | 155 | }
|
0 commit comments