-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exp4_Deque.c
213 lines (200 loc) · 4.74 KB
/
Exp4_Deque.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// SRUSHTI PATIL (IT - A - 43)
// Program to implement Double ended Queue in C.
# include<stdio.h>
# define Size 5
int deque_arr[Size];
int front = -1;
int rear = -1;
/*Begin of insert_rear*/
void insert_rear()
{
int added_item;
if((front == 0 && rear == Size-1) || (front == rear+1))
{ printf("Queue Overflow\n");
return;}
if (front == -1) /* if queue is initially empty */
{ front = 0;
rear = 0;}
else
if(rear == Size-1) /*rear is at last position of queue */
rear = 0;
else
rear = rear+1;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
deque_arr[rear] = added_item ;
}
/*End of insert_rear*/
/*Begin of insert_front*/
void insert_front()
{ int added_item;
if((front == 0 && rear == Size-1) || (front == rear+1))
{ printf("Queue Overflow \n");
return; }
if (front == -1)/*If queue is initially empty*/
{ front = 0;
rear = 0; }
else
if(front== 0)
front=Size-1;
else
front=front-1;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
deque_arr[front] = added_item ; }
/*End of insert_front*/
/*Begin of delete_front*/
void delete_front()
{ if (front == -1)
{ printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",deque_arr[front]);
if(front == rear) /*Queue has only one element */
{ front = -1;
rear=-1;
}
else
if(front == Size-1)
front = 0;
else
front = front+1;
}
/*End of delete_front*/
/*Begin of delete_rear*/
void delete_rear()
{
if (front == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",deque_arr[rear]);
if(front == rear) /*queue has only one element*/
{
front = -1;
rear=-1;
}
else
if(rear == 0)
rear=Size-1;
else
rear=rear-1; }
/*End of delete_rear*/
/*Begin of input_que*/
void display_queue()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{ printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if( front_pos <= rear_pos )
{
while(front_pos <= rear_pos)
{
printf("%d ",deque_arr[front_pos]);
front_pos++;
}
}
else
{
while(front_pos <= Size-1)
{ printf("%d ",deque_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",deque_arr[front_pos]);
front_pos++;
}
}
printf("\n");
}
/*End of display_queue*/
/*Begin of input_que*/
void input_que()
{ int choice;
do
{ printf("1.Insert at rear\n");
printf("2.Delete from front\n");
printf("3.Delete from rear\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{ case 1:
insert_rear();
break;
case 2:
delete_front();
break;
case 3:
delete_rear();
break;
case 4:
display_queue();
break;
case 5:
break;
default:
printf("Wrong choice\n");
}
}while(choice!=5);
}
/*End of input_que*/
/*Begin of output_que*/
void output_que()
{ int choice;
do
{ printf("1.Insert at rear\n");
printf("2.Insert at front\n");
printf("3.Delete from front\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_rear();
break;
case 2:
insert_front();
break;
case 3:
delete_front();
break;
case 4:
display_queue();
break;
case 5:
break;
default:
printf("Wrong choice\n");
}
}while(choice!=5);
}
/*End of output_que*/
/*Begin of main*/
main()
{ int choice;
printf("1.Input restricted dequeue\n");
printf("2.Output restricted dequeue\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
input_que();
break;
case 2:
output_que();
break;
default:
printf("Wrong choice\n");
}
}