-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPratical_3-3-revised.c
117 lines (106 loc) · 1.79 KB
/
Pratical_3-3-revised.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
#include<stdio.h>
# define N 5
int deque[N];
int f=-1;
int r=-1;
int count;
int isFull(){
int a=0;
if(count==N){
a=1;
}
return a;
}
int isEmpty(){
int a=0;
if(count==0){
a=1;
}
return a;
}
void enqueueFront(int x){
if(isFull()){
printf("the queue is full\n");
return;
}
if(f==0){
f=N-1;
}else{
f--;
deque[f]=x;
count++;
}
}
void enqueueRear(int x){
if(isFull()){
printf("the queue is full\n");
return;
}
r=(r+1)%N;
deque[r]=x;
count++;
}
void display(){
int i=f;
do{
if(deque[i]!=0){
printf("%d ",deque[i]);
}
i=(i+1)%N;
}while(i!=((r+1)%N)+1);
printf("\n");
}
int dequeueFront(){
if(isEmpty()){
printf("the queue is empty\n");
return -1;
}
int element = deque[f];
deque[f]=0;
f=(f+1)%N;
count--;
return element;
}
int dequeueRear(){
if(isEmpty()){
printf("the queue is empty\n");
return -1;
}
int element = deque[r];
deque[r]=0;
if(r==0){
r=N-1;
}else{
r--;
}
count--;
return element;
}
int main(){
int choice;
int ele;
do{
printf("Enter the choice\n");
printf("1.EnqueueFront\n 2.EnqueueRear\n 3.DequeueFront\n 4.DequeueRear\n 5.Display\n 6.exit\n");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the element\n");
scanf("%d",&ele);
enqueueFront(ele);
break;
case 2: printf("Enter the element\n");
scanf("%d",&ele);
enqueueRear(ele);
break;
case 3: ele = dequeueFront();
printf("The element deleted from front: %d \n",ele);
break;
case 4: ele = dequeueRear();
printf("The element deleted from rear: %d \n",ele);
break;
case 5: printf("the elements in the queue are ");
display();
}
}while(choice!=6);
return 0;
}