-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathqueue_using_array.c
More file actions
93 lines (78 loc) · 1.5 KB
/
queue_using_array.c
File metadata and controls
93 lines (78 loc) · 1.5 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
#include<stdio.h>
#define MAXSIZE 100
int arr[MAXSIZE], last = 0;
int isEmpty()
{
return (last<=0); //If last is reached to 0, then it is empty, return 1 else return 0
}
int isFull()
{
return (last>=MAXSIZE); //If last reached MAXSIZE then it is full, return 1, else return 0
}
int peek()
{
return arr[0];
}
void enqueue(int val)
{
if(isFull())
printf("No more space on Queue.\n");
else
arr[last++]=val;
}
void dequeue()
{
int i;
if(isEmpty())
{
printf("Queue is empty\n");
}
else
{
printf("Removed element: %d\n",peek());
//Shift All element left
for(i=1;i<last;i++)
arr[i-1] = arr[i];
last--;
}
}
void printlist()
{
int i;
if(last > 0)
{
printf("Queue Elements: \n");
for(i=0;i<last;i++)
printf("%d ",arr[i]);
puts("");
}
}
int main()
{
int action, val;
int exit = 0;
while(!exit)
{
printf("Enter your choice:\n");
printf("1. Enqueue\n2. Dequeue\n3. Exit\n");
scanf("%d", &action);
switch(action)
{
case 1:
scanf("%d", &val);
enqueue(val);
printlist();
puts("");
break;
case 2:
dequeue();
printlist();
puts("");
break;
case 3:
exit = 1;
break;
}
}
return 0;
}