-
Notifications
You must be signed in to change notification settings - Fork 1
/
doubly.c
129 lines (120 loc) · 2.27 KB
/
doubly.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
#include<stdio.h>
void Enqueue();
void Dequeue();
void Enqueuef();
void Dequeuer();
void Display();
int n,f=-1,r=-1,q[20];
int main() {
int x;
printf("Enter the limit limit of queue\n");
scanf("%d",&n);
loop:
printf("\nSelect a queue operation :\n1. Enqueue\n2. Dequeue\n3. Enqueue(front)\n4. Dequeue(rear)\n5. Display\n6. Exit\n\n");
scanf("%d",&x);
switch (x) {
case 1: Enqueue();
goto loop;
case 2: Dequeue();
goto loop;
case 3: Enqueuef();
goto loop;
case 4: Dequeuer();
goto loop;
case 5: Display();
goto loop;
case 6: return 0;
goto loop;
default: printf("Wrong selection, try again\n");
goto loop;
}
}
//Functions
void Enqueue() {
int a;
if(r==n-1||r+1==f)
printf("Queue overflow\n");
else if(r==-1) {
r=f=0;
printf("Enter the element to be inserted\n");
scanf("%d",&a);
q[r]=a;
}
else {
r=r+1;
printf("Enter the element to be inserted\n");
scanf("%d",&a);
q[r]=a;
}
}
void Dequeue() {
if(f==-1)
printf("Queue underflow\n");
else if(f==r) {
printf("Element %d has been deleted\n",q[f]);
f=r=-1;
}
else if(f==n-1) {
printf("Element %d has been deleted\n",q[f]);
f=0;
}
else {
printf("Element %d has been deleted\n",q[f]);
f++;
}
}
void Enqueuef() {
int a;
if(f-1==r||(f==0&&r==n-1))
printf("Queue overflow\n");
else if(f==0) {
f=n-1;
printf("Enter the element to be inserted\n");
scanf("%d",&a);
q[f]=a;
}
else if(f==-1) {
f=r=0;
printf("Enter the element to be inserted\n");
scanf("%d",&a);
q[f]=a;
}
else {
f--;
printf("Enter the element to be inserted\n");
scanf("%d",&a);
q[f]=a;
}
}
void Dequeuer() {
if(f==-1)
printf("Queue underflow\n");
else if(f==r) {
printf("Element %d has been deleted\n",q[r]);
f=r=-1;
}
else if(f>r&&r==0) {
printf("Element %d has been deleted\n",q[r]);
r=n-1;
}
else {
printf("Element %d has been deleted\n",q[r]);
r--;
}
}
void Display() {
int i;
printf("Queue is :\n");
if(f>r) {
for(i=f;i<n;i++)
printf("%d ",q[i]);
for(i=0;i<=r;i++)
printf("%d ",q[i]);
printf("\n");
}
else {
for(i=f;i<=r;i++)
printf("%d ",q[i]);
printf("\n");
}
}