-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubleEndedQueue.c
More file actions
168 lines (146 loc) · 3.67 KB
/
DoubleEndedQueue.c
File metadata and controls
168 lines (146 loc) · 3.67 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
157
158
159
160
161
162
163
164
165
166
167
168
//11 a)
#include <stdio.h>
#include <stdlib.h>
typedef struct Deque{
int capacity;
int*dq;
int front;
int rear;
}Deque;
void enqueue_front(Deque*p){
if(p->rear==p->capacity-1){
printf("Resizing...\n");
int*newarr=(int*)realloc(p->dq,p->capacity*2*sizeof(int));
if(newarr==NULL){
printf("Memory allocation failed...\n");
return;
}
p->dq=newarr;
p->capacity*=2;
}
int val;
printf("Enter the value to enqueue at front: ");
scanf("%d",&val);
if(p->front==-1){
p->front=p->rear=0;
p->dq[p->front]=val;
}
else if(p->front>0){
p->dq[--p->front]=val;
}
else if(p->front==0){
p->rear++;
for(int i=p->rear;i>0;i--){
p->dq[i]=p->dq[i-1];
}
p->dq[0]=val;
}
}
void display(Deque*p){
if(p->front==-1){
printf("Queue Empty\n");
return;
}
printf("Queue:\t");
for(int i=p->front;i!=p->rear;i++){
printf("%d ",p->dq[i]);
}
printf("%d\n",p->dq[p->rear]);
}
void enqueue_rear(Deque*p){
if(p->rear==p->capacity-1){
printf("Resizing...\n");
int*newarr=(int*)realloc(p->dq,p->capacity*2*sizeof(int));
if(newarr==NULL){
printf("Memory allocation failed...\n");
return;
}
p->dq=newarr;
p->capacity*=2;
}
int val;
printf("Enter the value to enqueue at rear: ");
scanf("%d",&val);
if(p->front==-1){
p->front=0;
}
p->dq[++p->rear]=val;
}
void dequeue_front(Deque*p){
if(p->front==-1){
printf("Queue Underflow\n");
return;
}
printf("Dequeued value: %d\n",p->dq[p->front]);
if(p->front==p->rear){
p->front=p->rear=-1;
return;
}
for(int i=p->front;i<p->rear;i++){
p->dq[i]=p->dq[i+1];
}
p->rear--;
}
void dequeue_rear(Deque*p){
if(p->front==-1){
printf("Queue Underflow\n");
return;
}
printf("Dequeued value: %d\n",p->dq[p->rear]);
if(p->rear==p->front){
p->rear=p->front=-1;
return;
}
p->rear--;
}
void peek(Deque*p){
if(p->front==-1){
printf("Queue Underflow\n");
return;
}
printf("First element of Queue: %d\n",p->dq[p->front]);
}
void main(){
int ch;
Deque*p=(Deque*)malloc(sizeof(Deque));
if(p==NULL){
printf("Memory Allocation Failed...\n");
return;
}
p->front=p->rear=-1;
printf("\nEnter the maximum capacity of the double ended queue: ");
scanf("%d",&p->capacity);
p->dq=(int*)calloc(p->capacity,sizeof(int));
for(;;){
printf("\nENTER:\n1. To insert at the front end\n2. To insert at the rear end\n3. To display\n4. To delete at the front end\n5. To delete at the rear end\n6. To peek\n0. To Exit\n");
printf("Enter your choice from above: ");
scanf("%d",&ch);
switch(ch){
case 1:
enqueue_front(p);
break;
case 2:
enqueue_rear(p);
break;
case 3:
display(p);
break;
case 4:
dequeue_front(p);
break;
case 5:
dequeue_rear(p);
break;
case 6:
peek(p);
break;
case 0:
printf("Exiting...\n");
free(p->dq);
free(p);
exit(0);
default:
printf("Invalid Choice! Please Enter Again!\n");
}
}
}