-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue using array
80 lines (80 loc) · 2.01 KB
/
queue using array
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
#include <stdio.h>
#define MAX 20
#include<stdlib.h>
int Queue_from_array[MAX];
int rear = - 1;
int front = - 1;
void insert_queue()
{
int add_element;
if (rear == MAX - 1)
printf("Overflow\n\n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Enter element: \n");
scanf("%d", &add_element);
printf("%d is inserted in queue\n\n",add_element);
rear = rear + 1;
Queue_from_array[rear] = add_element;
}
}
void delete_queue()
{
if (front == - 1 || front > rear)
{
printf("Underflow\n\n");
return ;
}
else
{
printf("Deleted element : %d\n\n", Queue_from_array[front]);
front = front + 1;
}
}
void display_queue()
{
int i;
if (front == - 1)
printf("Empty queue!\n\n");
else
{
printf("Queue is : ");
for (i = front; i <= rear; i++)
printf("%d ", Queue_from_array[i]);
printf("\n");
}
}
int main()
{
printf("LIST OF OPERATIONS\n\n");
printf("\t 1. Insert element: \n");
printf("\t 2. Delete element: \n");
printf("\t 3. Display queue\\n");
printf("\t 4. Exit\n");
int op;
while (1)
{
printf("\nChoose an operation number: ");
scanf("%d", &op);
switch(op)
{
case 1:
insert_queue();
break;
case 2:
delete_queue();
break;
case 3:
display_queue();
break;
case 4:
exit(1);
default:
printf("This is an invalid operation\n");
}
}
return 0;
}