-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15_linear_queue.c
90 lines (82 loc) · 1.88 KB
/
15_linear_queue.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
#include<stdio.h>
#define MAX 50
int QUEUE[MAX];
int front, rear;
front = -1, rear = -1;
// Function to Enqueue Item
void enqueue()
{
int item;
if (rear == (MAX - 1))
printf("\nQueue Overflow\n\n");
else
{
if (rear == -1)
front = rear = 0;
else
rear++;
printf("\nEnter Element : ");
scanf("%d", &item);
QUEUE[rear] = item;
printf("Element Enqueued Successfully\n\n");
}
}
// Function to Dequeue
void dequeue()
{
if (front == -1)
printf("\nQueue Underflow\n\n");
else
{
printf("\nDequeued Item : %d", QUEUE[front]);
if (front == rear)
front = rear = -1;
else
front++;
printf("\nElement Dequeued Successfully\n\n");
}
}
// Function to Peek into Queue
void peek()
{
if (front == -1)
printf("\nQueue Underflow\n\n");
else
printf("\nLast Element in Queue : %d\n\n", QUEUE[front]);
}
/*
// Function to Display Queue
void display()
{
int index, index1 = 1;
printf("\nEntered Element\n");
for(index = front ; index <= rear ; index++)
printf("Element %d : %d\n", index1++, QUEUE[index]);
printf("\n");
}
*/
int main()
{
int choice;
while(1)
{
printf("Enter\n");
printf("1. To Enqueue an Element\n");
printf("2. To Dequeue an Element\n");
printf("3. To Peek into the Queue\n");
// printf("4. Display Queue\n");
printf("0. To Exit\n");
printf("Enter Your Choice : ");
scanf("%d", &choice);
switch(choice)
{
case 0 : return 0;
case 1 : enqueue(); break;
case 2 : dequeue(); break;
case 3 : peek(); break;
// case 4 : display(); break;
default : printf("\nInvalid Choice, Try Again\n\n");
}
}
return 0;
}