-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCircularQueues.c
128 lines (120 loc) · 3.01 KB
/
CircularQueues.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
124
125
126
127
128
#include<stdio.h>
#include<conio.h>
#define MAX 5
int queue[MAX];
int front = -1;
int rear = -1;
/*
We came with the concept of circular queuessimply because memory was being wasted in linear queue.
*/
/* Function to enter element into a circular queue */
void enqueue(int element){
// Added the statement (front == rear + 1), this statement arrives as vijay told...
if((front==0 && rear == MAX-1) || (front == rear +1))
printf("\nOVERFLOW");
else if(front == -1){
front =0;
rear =0;
queue[rear]=element;
}
else if((front != 0) && (rear == MAX-1) ){
rear =0;
queue[rear] = element;
}
else{
rear++;
queue[rear] = element;
}
}
int dequeue(){
int val;
if(front == -1){
printf("\nUNDERFLOW");
return -1;
}
else if(front == rear){
val = queue[front];
front = rear = -1;
return val;
}
else if(front == MAX-1){
val = queue[front];
front =0;
return val;
}
else{
val = queue[front];
front++;
return val;
}
}
int peak(){
if(front == -1){
printf("\nQueue is empty");
return -1;
}
else{
return queue[front];
}
}
void display(){
if(front == -1){
printf("\nQueue is empty");
}
else if(rear>=front){
printf("\nThe elements are: \t");
for(int i=front;i<=rear;i++){
printf("%d\t",queue[i]);
}
}
else{
printf("\nThe elements are: \t");
for(int i=front;i<MAX;i++){
printf("%d\t",queue[i]);
}
for(int i=0;i<=rear;i++){
printf("%d\t",queue[i]);
}
}
}
void main(){
int option;
do{
printf("\n**************************MENU***********************");
printf("\n1. ENQUEUE");
printf("\n 2. DEQUEUE");
printf("\n 3. PEAK");
printf("\n 4. DISPLAY");
printf("\n 5. EXIT!");
printf("\nEnter your choice:\t");
scanf("%d",&option);
int num1,num2,num3;
switch(option){
case 1:
printf("\nEnter the element you wanna enqueue:\t");
scanf("%d",&num1);
enqueue(num1);
break;
case 2:
num2 = dequeue();
if(num2 != -1){
printf("\nThe element dequeued id %d",num2);
}
break;
case 3:
num3 = peak();
if(num3 != -1){
printf("\nThe element at the peak is %d",num3);
}
break;
case 4:
display();
break;
case 5:
break;
default:
printf("\nINVALID CHOICE");
}
}while(option != 5);
//printf("Hi");(Show break takes outside of while loop)
}