This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p11_2809.c
160 lines (142 loc) · 2.16 KB
/
p11_2809.c
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
// Emmanuel Jojy
// S3 CSE A
// Roll no: 53
// Prg 11/2809
// Double Ended Queue Implementation
// Static Array
#include<stdio.h>
#define MAXSIZE 5
//Global variables
int DQ[MAXSIZE];
int FRONT = -1;
int REAR = -1;
void dqueue();
void pushdq();
void popdq();
void inject();
void eject();
void display();
void main()
{
dqueue();
}
void dqueue()
{
int ch;
while(1)
{
printf("\n\n---- DE-QUEUE OPERATIONS ----");
printf("\n1. PUSHDQ\t2. POPDQ\t3. INJECT\t4. EJECT\t5. DISPLAY\t6. EXIT");
printf("\nChoice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: pushdq(); break;
case 2: popdq(); break;
case 3: inject(); break;
case 4: eject(); break;
case 5: display(); break;
default: return;
}
}
}
void pushdq()
{
int ITEM, temp;
printf("\n Enter element to enqueue: ");
scanf("%d", &ITEM);
if(FRONT == -1 && REAR == -1)
{
FRONT = 0;
REAR = 0;
DQ[FRONT] = ITEM;
return;
}
//FRONT at 0
temp = FRONT == 0 ? MAXSIZE - 1 : FRONT - 1;
if(temp == REAR)
{
printf(" DQ is full!\n");
return;
}
FRONT = temp;
DQ[FRONT] = ITEM;
}
void popdq()
{
int ITEM;
if(FRONT == -1 && REAR == -1)
{
printf("\n DQ is empty!\n");
return;
}
ITEM = DQ[FRONT];
printf("\n POPDQ %d SUCCESSFULLY", ITEM);
if(FRONT == REAR)
{
FRONT = -1;
REAR = -1;
}
else
{
FRONT = (FRONT + 1) % MAXSIZE;
}
}
void inject()
{
int ITEM;
printf("\n Enter element to inject: ");
scanf("%d", &ITEM);
if((REAR + 1) % MAXSIZE == FRONT)
{
printf("\n DQ IS FULL");
return;
}
if(FRONT == -1 && REAR == -1)
{
FRONT = 0;
REAR = 0;
DQ[REAR] = ITEM;
}
else
{
REAR = (REAR + 1) % MAXSIZE;
DQ[REAR] = ITEM;
}
}
void eject()
{
int ITEM;
if(FRONT == -1 && REAR == -1)
{
printf("\n DQ is Empty");
return;
}
if(FRONT == REAR)
{
ITEM = DQ[REAR];
FRONT = -1;
REAR = -1;
}
else
{
ITEM = DQ[REAR];
REAR = REAR == 0 ? MAXSIZE - 1 : REAR - 1;
}
printf("\n EJECTED %d SUCCESSFULLY", ITEM);
}
void display()
{
if(FRONT == -1 && REAR == -1)
{
printf("\n DE - QUEUE IS EMPTY\n");
return;
}
printf("\n\t");
int i;
for(i = FRONT; i != REAR; i = (i + 1) % MAXSIZE)
{
printf("%d\t", DQ[i]);
}
printf("%d\n", DQ[i]);
}