forked from thisisshub/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircular_Queue.c
More file actions
88 lines (82 loc) · 1.74 KB
/
Circular_Queue.c
File metadata and controls
88 lines (82 loc) · 1.74 KB
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
#include<stdio.h>
#define MAXSIZE 10
int front=-1, rear=-1,cqueue[MAXSIZE], item;
void enqueue();
void dequeue();
void display();
int main()
{
int choice;
char ch;
do{
printf("\n 1. ENQUEUE");
printf("\n 2. DEQUEUE");
printf("\n 3. DISPLAY");
printf("\n 4. ENTER YOUR CHOICE:");
scanf("%d", &choice);
switch(choice){
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3: display();
break;
default: printf("\n Wrong Choice.");
break;
}
printf("Do you want to continue:");
scanf("%c",&ch);
scanf("%c",&ch);
}
while(ch=='y');
return 0;
}
void enqueue()
{
int item;
printf("Inset the element in queue : ");
scanf("%d", &item);
if (front==(rear+1)%MAXSIZE)
printf("circular Queue Overflow \n");
else if (front == -1 && rear == -1 ) //If queue is initially empty
{ front = rear = 0;
cqueue[rear] = item;
}
else
{
rear = (rear + 1)%MAXSIZE;
cqueue[rear] = item;
}
}
void dequeue()
{
if (front == - 1)
{
printf("circular Queue Underflow \n");
}
else if(front==rear)
{
front = rear = -1;
}
else
{
printf("Deleted Element is : %d\n", cqueue[front]);
front = (front + 1)%MAXSIZE;
}
}
void display()
{
int i;
if(front==-1 && rear==-1)
{
printf("The queue is Empty\n");
}
else
{
for(i= front; i!=rear;i=(i+1)%MAXSIZE)
{
printf("%d ",cqueue[i]);
}
printf("%d ",cqueue[i]);
}
}