luc064.c ​
Metadata ​
- Author — Amit Dutta
- Last updated — 08 Feb 2026
- License — MIT License (See the LICENSE file for details)
Problem Statement ​
Problem Statement
Simulate a Dequeue (Double Ended Queue) using an array. Support: retrieve left, retrieve right, insert left, insert right. Use pointers left and right.
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define MAX 10
void insert_left(int *, int *, int *, int);
void insert_right(int *, int *, int *, int);
void retrieve_left(int *, int *, int *);
void retrieve_right(int *, int *, int *);
void display(int *, int, int);
int main()
{
int dq[MAX];
int left = -1, right = -1;
int choice, val;
while (1)
{
printf("\n--- Dequeue Menu ---\n");
printf("1. Insert Left\n2. Insert Right\n");
printf("3. Retrieve Left\n4. Retrieve Right\n");
printf("5. Display\n6. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter value: ");
scanf("%d", &val);
insert_left(dq, &left, &right, val);
break;
case 2:
printf("Enter value: ");
scanf("%d", &val);
insert_right(dq, &left, &right, val);
break;
case 3:
retrieve_left(dq, &left, &right);
break;
case 4:
retrieve_right(dq, &left, &right);
break;
case 5:
display(dq, left, right);
break;
case 6:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
void insert_left(int *dq, int *left, int *right, int val)
{
// Check if full
if ((*left == 0 && *right == MAX - 1) || (*left == *right + 1))
{
printf("Overflow! Dequeue is full.\n");
return;
}
if (*left == -1) // Initially empty
{
*left = 0;
*right = 0;
}
else if (*left == 0) // Wrap around
*left = MAX - 1;
else
(*left)--;
dq[*left] = val;
printf("Inserted %d at Left.\n", val);
}
void insert_right(int *dq, int *left, int *right, int val)
{
if ((*left == 0 && *right == MAX - 1) || (*left == *right + 1))
{
printf("Overflow! Dequeue is full.\n");
return;
}
if (*left == -1) // Initially empty
{
*left = 0;
*right = 0;
}
else if (*right == MAX - 1) // Wrap around
*right = 0;
else
(*right)++;
dq[*right] = val;
printf("Inserted %d at Right.\n", val);
}
void retrieve_left(int *dq, int *left, int *right)
{
if (*left == -1)
{
printf("Underflow! Dequeue is empty.\n");
return;
}
printf("Retrieved from Left: %d\n", dq[*left]);
if (*left == *right) // Only one element was present
{
*left = -1;
*right = -1;
}
else if (*left == MAX - 1)
*left = 0;
else
(*left)++;
}
void retrieve_right(int *dq, int *left, int *right)
{
if (*left == -1)
{
printf("Underflow! Dequeue is empty.\n");
return;
}
printf("Retrieved from Right: %d\n", dq[*right]);
if (*left == *right) // Only one element was present
{
*left = -1;
*right = -1;
}
else if (*right == 0)
*right = MAX - 1;
else
(*right)--;
}
void display(int *dq, int left, int right)
{
int i;
if (left == -1)
{
printf("Dequeue is Empty\n");
return;
}
printf("Elements: ");
i = left;
while (1)
{
printf("%d ", dq[i]);
if (i == right)
break;
if (i == MAX - 1)
i = 0;
else
i++;
}
printf("\n");
}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
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