Repository navigation
Expand file tree
/
Copy pathqueuewitharray.c
More file actions
77 lines (71 loc) · 1.43 KB
/
Copy pathqueuewitharray.c
File metadata and controls
77 lines (71 loc) · 1.43 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
#include <stdio.h>
#include <stdlib.h>
struct Queue{
int size;
int front;
int back;
int *arr;
};
int isEmpty(struct Queue *ptr){
if (ptr->back == ptr->front){
return 1;
}
return 0;
}
// (*ptr).top
int isFull(struct Queue *ptr){
if (ptr->back==ptr->size-1){
return 1;
}
return 0;
}
int enqueue(struct Queue *q,int value){
if (isFull(q)){
printf("Queue overflow\n");
}
else{
q->back++;
q->arr[q->back]=value;
}
}
int dequeue(struct Queue *q){
int a=-1;
if (isEmpty(q)){
printf("Queue underflow\n");
}
else{
q->front++;
a=q->arr[q->front];
for (int i = q->front + 1; i <= q->back; i++) {
q->arr[i - 1] = q->arr[i];
}
q->back--;
q->front--;
}
return a;
}
int main(){
struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue));
q->size=6;
q->back=-1;
q->front=-1;
q->arr=(int *)malloc(q->size*sizeof(int));
enqueue(q,23);
enqueue(q,45);
enqueue(q,54);
enqueue(q,65);
enqueue(q,50);
enqueue(q,100);
printf("here's the first dequeued value: %d\n",dequeue(q));
printf("here's the second dequeued value: %d\n",dequeue(q));
if (isEmpty(q)){
printf("Queue underflow\n");
}
else if (isFull(q)){
printf("Queue overflow\n");
}
else{
printf("Everything normal\n");
}
return 0;
}