-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearQueue.c
More file actions
129 lines (103 loc) · 2.15 KB
/
LinearQueue.c
File metadata and controls
129 lines (103 loc) · 2.15 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
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
// Linear Queue
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
int front = -1;
int rear = -1;
int queue[SIZE];
// Enqueue Operation
int enqueue(int val)
{
if (rear == SIZE - 1)
{
printf("Queue Is Over Flow..\n");
return -1;
}
if (front == -1) // first element insert
front = 0;
rear++; // rear must increase ALWAYS
queue[rear] = val;
printf("Inserted %d\n", val);
return val;
}
// Dequeue Operation
int dequeue()
{
if (front == -1 || front > rear)
{
printf("The Queue Is Empty..\n");
return -1;
}
int val = queue[front];
front++;
// queue becomes empty again
if (front > rear)
{
front = rear = -1;
}
printf("Deleted %d\n", val);
return val;
}
// Peek Operation
int peek()
{
if (front == -1)
{
printf("The Queue Is Empty...\n");
return -1;
}
printf("Front Element = %d\n", queue[front]);
return queue[front];
}
// Display Operation
int display()
{
if (front == -1)
{
printf("The Queue Is Empty....\n");
return -1;
}
printf("Queue Elements: ");
for (int i = front; i <= rear; i++)
{
printf("%d ", queue[i]);
}
printf("\n");
}
int main()
{
int choice, val;
while (1)
{
printf("\n<----Queue Menu--->");
printf("\n 1.Enqueue");
printf("\n 2.Dequeue");
printf("\n 3.Peek");
printf("\n 4.Display");
printf("\n 5.Exit");
printf("\nEnter the Choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the Element in the Queue:");
scanf("%d", &val);
enqueue(val);
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Invalid Choice.....");
}
}
return 0;
}