forked from iamAnki/CPP-Programs-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.c
More file actions
99 lines (91 loc) · 1.55 KB
/
Queue.c
File metadata and controls
99 lines (91 loc) · 1.55 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
93
94
95
96
97
98
99
#include <stdio.h>
#include <stdlib.h>
struct queue
{
int r, f;
int *arr;
int size;
};
struct queue q;
//perform traversal of elements
void traverse()
{
printf("The elements in queue are: ");
int i;
for (i = q.f + 1; i <= q.r; i++)
{
printf("%d ", q.arr[i]);
}
printf("\n");
}
//check if queue is empty to satisfy the underflow condition
int is_empty()
{
if (q.r == q.f)
{
return 1;
}
return 0;
}
//check if queue is empty to satisfy the overflow condition
int is_full()
{
if (q.r == q.size - 1)
{
return 1;
}
return 0;
}
//perform insertion of elements in queue
void enqueue(int val)
{
if (is_full())
{
printf("queue overflow!!");
}
else
{
q.r++;
q.arr[q.r] = val;
}
}
//perform deletion of elements in queue
int dequeue()
{
if (is_empty())
{
printf("queue underflow!!");
}
else
{
int val;
q.f++;
val = q.arr[q.f];
return val;
}
}
int main()
{
int n;
printf("Enter the size: ");
scanf("%d",&n);
q.f = q.r = -1;
q.size = n;
q.arr = (int *)malloc(q.size * sizeof(int));
int i,d;
printf("Enter the number of elements to enter in a queue: ");
scanf("%d",&d);
for (i = 0; i < d; i++)
{
int x;
scanf("%d", &x);
enqueue(x);
traverse();
}
printf("\n");
printf("Element dequeue is %d\n", dequeue());
traverse();
printf("Element dequeue is %d\n", dequeue());
traverse();
return 0;
}