-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularQueue.c
116 lines (109 loc) · 2.96 KB
/
CircularQueue.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
//CircularQueue code
#include<stdio.h>
#include<stdlib.h>
struct CircularQueue{
int front,rear ;
int capacity;
int *array ;
};
struct CircularQueue* CreateQueue(int capacity){
struct CircularQueue * Q = malloc(sizeof(struct CircularQueue));
if(!Q) // if by some reason malloc not works then do a NULL return
return(NULL);
Q->capacity = capacity ;
Q->front = -1 ;
Q->rear = -1 ;
Q->array = malloc(Q->capacity*sizeof(int));
if(!Q->array) // if by some reason malloc not works then do a NULL return
return(NULL);
return(Q);
}
int isEmptyQueue(struct CircularQueue* Q){
return(Q->front==-1);
}
int isFullQueue(struct CircularQueue *Q){
// this is also another suitable conditional check (both can be used independently)
return((Q->rear+1)%Q->capacity==Q->front); //if(front==0||rear = capacity - 1) || (front = rear + 1)
}
int QueueSize(struct CircularQueue* Q){
//if(front<=rear)
//elements = rearIndex + 1
//else
//elements = frontIndex to lastIndex + zeroIndex to rearIndex
return((Q->capacity-Q->front+Q->rear+1)%Q->capacity);
}
void enqueue(struct CircularQueue *Q, int data){
if(isFullQueue(Q))
printf("overflow");
else{
// Q->rear++;
// if Q->rear == capacity
// Q-rear = 0
// Q-array[Q->rear] = data ;
// if(Q-front == -1)
// Q->front = Q->rear ;
Q->rear = (Q->rear+1)%Q->capacity;
Q->array[Q->rear] = data ;
if(Q->front == -1)
Q->front = Q->rear;
}
}
int dequeue(struct CircularQueue *Q){
int data = -1 ;
if(isEmptyQueue(Q)){
printf("Queue is empty");
return(-1);
}
else{
data = Q->array[Q->front];
if(Q->front == Q->rear)
Q->front = Q->rear = -1 ;
else
// if(front<rear)
// Q->front++;
// else
// Q->front++;
// Q->front == capacity ;
// Q->front = 0 ;
Q->front = (Q->front +1)%Q->capacity;
}
return(data);
}
void main(){
int choice , item ;
struct CircularQueue* Queue = CreateQueue(3);
while(1){
printf("1.Insert 2.Delete 3.Display\nEnter your Choice\n" );
scanf("\n%d",&choice);
switch(choice){
case 1:
printf("enter a number\n");
scanf("%d",&item);
enqueue(Queue,item);
break;
case 2:
dequeue(Queue);
break;
case 3:
if(Queue->front <= Queue->rear){
if ((Queue->front == Queue->rear) && (Queue->front == -1)){
printf("Queue is empty");
break;
}
for(int i = Queue->front ; i <= Queue->rear ; i++){
printf(" %d\n",Queue->array[i]);
}
}
else{
for(int i = 0 ; i <= Queue->rear ;i++ ){
printf(" %d\n",Queue->array[i]);
}
for(int i = Queue->front ; i < Queue->capacity ;i++ )
{
printf(" %d\n",Queue->array[i]);
}
}
break;
}
}
}