-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathqueue.c
119 lines (110 loc) · 2.44 KB
/
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
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
#include <stdio.h>
#define MAXSIZE 10
int queue[MAXSIZE], front = -1, rear = -1;
void Enqueue(int element);
int Dequeue();
int Front();
void Display();
int is_full();
int is_empty();
void main()
{
int choice, element, flag = 1, temp;
while (flag)
{ printf("\n\nQueue Operations");
printf("\n----------------");
printf("\n1. Enqueue");
printf("\n2. Dequeue");
printf("\n3. Front");
printf("\n4. Display");
printf("\n5. Exit");
printf("\n----------------");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1 :
if(!is_full())
{
printf("\nEnter the element to be enqueued: ");
scanf("%d", &element);
Enqueue(element);
printf("\nEnqueued element: %d", element);
}else{
printf("\nError: Queue is full");
}
break;
case 2 :
if(!is_empty())
{
temp = Dequeue();
printf("\nDequeued element: %d", temp);
} else {
printf("\nError: Queue is empty");
}
break;
case 3:
if(!is_empty()){
temp = Front();
printf("\nFront element: %d", temp);
}
break;
case 4:
Display();
break;
case 5:
flag = 0;
break;
default:
printf("\nInvalid Choice");
}
}
}
int is_full()
{
if(rear == (MAXSIZE - 1)) return 1;
return 0;
}
int is_empty(){
if(front == -1 && rear == -1) return 1;
return 0;
}
void Enqueue(int element)
{
if(is_empty())
{
front = rear = 0;
} else {
rear++;
}
queue[rear] = element;
}
int Dequeue()
{
int temp;
if(front == rear){
temp = queue[front];
front = rear = -1;
} else {
temp = queue[front];
front++;
}
return temp;
}
int Front()
{
return queue[front];
}
void Display()
{
int i;
printf("\nStatus of the queue:\n");
if(is_empty())
{
printf("\nQueue is Empty");
return;
}
for (i = front; i <= rear; ++i) {
printf("%d\t", queue[i]);
}
}