-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.c
123 lines (116 loc) · 2.34 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
120
121
122
123
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 5
int A[MAX_SIZE];
int front = -1, rear = -1;
int IsEmpty()
{
if (front == -1 && rear == -1)
{
return 1;
}
else
return 0;
}
int IsFull()
{
if ((rear+1) % MAX_SIZE == front)
{
return 1;
}
else
return 0;
}
void Enqueue(int x)
{
if(IsFull())
{
printf("Error: Queue is Full\n");
return;
}
if (IsEmpty())
{
front = rear = 0;
}
else
{
rear = (rear+1) % MAX_SIZE;
}
A[rear] = x;
}
void Dequeue()
{
if(IsEmpty())
{
printf("Error: Queue is Empty\n");
return;
}
else
{
front = (front+1) % MAX_SIZE;
}
}
int Front()
{
if(front == -1)
{
printf("Error: empty queue\n");
return -1;
}
return A[front];
}
int QueueSize()
{
int count =-1;
count = (rear + MAX_SIZE - front) % MAX_SIZE + 1;
return count;
}
void Print()
{
int count = QueueSize();
printf("Queue is as follows : ");
for(int i = 0; i <count; i++)
{
int index = (front+i) % MAX_SIZE;
printf("%d ",A[index]);
}
printf("\n\n");
}
int main()
{
int ch,n;
printf("Enter your choice\n");
while (1)
{
printf("1 to insert element\n2 to delete Element\n3 to return front element\n4 to find size of queue\n5 to check if queue is empty or full\n6 to Display elements\n7 to exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter data to be inserted\n");
scanf("%d",&n);
Enqueue(n);
if(!IsFull())
printf("Data %d inserted Successfully\n",n);
break;
case 2:Dequeue();
break;
case 3:printf("%d\n",Front());
break;
case 4:printf("%d\n",QueueSize());
break;
case 5:if (IsEmpty() == 1)
printf("Queue is Empty\n");
else if (IsFull())
printf("Queue is Full\n");
else
printf("Queue is not empty\n");
break;
case 6:Print();
break;
case 7:exit(1);
break;
default:printf("Enter Valid option\n");
break;
}
}
}