-
Notifications
You must be signed in to change notification settings - Fork 0
/
47_DEQueue_using_Array.c
109 lines (92 loc) · 1.92 KB
/
47_DEQueue_using_Array.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
#include<stdio.h>
#include<stdlib.h>
typedef struct queue{
int size, front, rear;
int * arr;
}queue;
void queueTraverse(queue* q){
printf("Queue elements:\n" );
for(int i=0; i<=q->rear; i++){
printf("%d ", q->arr[i]);
}
printf("\n");
}
int isFull(queue* q){
if(q->rear==q->size-1){
return 1;
}
return 0;
}
int isEmpty(queue* q){
if(q->rear==q->front){
return 1;
}
return 0;
}
void enqueueR(queue* q, int val){
if(isFull(q)){
printf("Queue is full!\n");
}
else{
q->rear++;
q->arr[q->rear] = val;
}
}
int dequeueF(queue* q){
int a;
if(isEmpty(q)){
printf("Queue is empty!\n");
}
else{
q->front++;
a = q->arr[q->front];
}
return a;
}
void enqueueF(queue * q, int val){
if(isFull(q) || q->front==-1){
printf("Queue is full!\n");
}
else{
q->arr[q->front] = val;
q->front--;
}
}
int dequeueR(queue * q){
int val;
if(isEmpty(q)){
printf("Queue is empty!\n");
}
else{
val = q->arr[q->rear];
q->rear--;
return val;
}
}
int main(){
queue * q = (queue *)malloc(sizeof(queue));
q->size = 10;
q->front = q->rear = -1;
q->arr = (int*)malloc(q->size*sizeof(int));
//Insert elements from rear in the queue using enqueue() method
enqueueR(q, 31);
enqueueR(q, 14);
enqueueR(q, 65);
enqueueR(q, 42);
enqueueR(q, 78);
enqueueR(q, 11);
enqueueR(q, 26);
queueTraverse(q);
//Delete elements from front in the queue using dequeue() method
printf("Dequeue element is %d\n", dequeueF(q));
printf("Dequeue element is %d\n", dequeueF(q));
printf("Dequeue element is %d\n", dequeueF(q));
enqueueF(q, 87);
enqueueF(q, 35);
enqueueF(q, 32);
queueTraverse(q);
if(isEmpty(q)){
printf("Queue is empty\n");
}
return 0;
}