-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_queue.c
More file actions
92 lines (89 loc) · 1.82 KB
/
Copy pathlinear_queue.c
File metadata and controls
92 lines (89 loc) · 1.82 KB
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
#include <stdio.h>
#define size 10
typedef struct
{
int a[size];
int f;
int r;
}queue_type;
void initial(queue_type *v){
v->f=-1;
v->r=-1;
}
int overflow(queue_type *v)
{
if (v->f==0 && v->r==size-1)
return 0;
else
return 1;
}
int underflow(queue_type *v)
{
if (v->f == -1 && v->r==-1)
return 0;
else
return 1;
}
int enqueue(queue_type *v)
{
int ele;
printf("Enter the element\n");
scanf("%d", &ele);
if(v->f==-1 &&v->r==-1){
v->f=0;
v->r=0;
}
else
v->r++;
v->a[v->r]=ele;
}
int dequeue(queue_type *v)
{
int temp = v->a[v->f];
if(v->f==v->r){
v->f=-1;
v->r=-1;
}
else
v->f++;
return temp;
}
int main()
{
queue_type s;
initial(&s);
int n,o,u,d;
char ch;
do
{
printf("Choose one of the following option\n1.Enqueue\n2.Dequeue\n");
scanf("%d", &n);
switch(n)
{
case 1:
o=overflow(&s);
if(o==0){
printf("The queue have overflowed\n");
break;}
else
enqueue(&s);
break;
case 2:
u=underflow(&s);
if(u==0)
printf("The queue have underflowed\n");
else{
d=dequeue(&s);
printf("%d is deleted from the queue\n",d);
}
break;
default:
printf("Invalid choice");
}
printf("To continue press y\nTo discontinue press n\n");
scanf("\n%c",&ch);
if(ch=='n')
printf("You are out of the program");
}while (ch=='y');
return 0;
}