-
Notifications
You must be signed in to change notification settings - Fork 50
/
Queue_Operations.c
118 lines (102 loc) · 1.54 KB
/
Queue_Operations.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
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
/*
Write a program to implement QUEUE using arrays that performs following operations
(a) INSERT (b) DELETE (c) DISPLAY
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX 3
void QPUSH();
void Display();
void QPOP();
int queue[MAX];
int rear = -1,front = -1;
void QPUSH()
{
int element;
if (rear == MAX-1)
{
printf("\n Queue is Overflow.");
}
else
{
printf("\n Enter element to be inserted in a queue.");
scanf("%d",&element);
if (front==-1)
{
front=rear=0;
}
else
{
rear++;
}
queue[rear]=element;
printf("\n inserted element is: %d",element);
}
}
void QPOP()
{
int del_element;
if (front == -1)
{
printf("\n Queue is Underflow.\n");
}
else
{
del_element=queue[front];
if (front==rear)
{
front=rear=-1;
}
else
{
front++;
}
printf("\n Element Deleted. %d \n",del_element);
}
}
void Display()
{
if (front==-1)
{
printf("\n Queue is Empty.\n");
}
else
{
for (int i = front; i <= rear ; i++)
{
printf("\n %d\t ",queue[i]);
}
}
}
int main()
{
int choice;
while(1)
{
printf("\n\n 1. Insert element to queue. \n 2. Delete element from queue \n 3. Display data in a queue. \n 4. Exit");
printf("\n\n Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
// printf("\n Insert");
QPUSH();
break;
case 2:
// printf("\n Delete");
QPOP();
break;
case 3:
// printf("\n Display");
Display();
break;
case 4:
exit(0);
break;
default:
printf("\n Enter valid choice.");
break;
}
}
return 0;
}