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