-
Notifications
You must be signed in to change notification settings - Fork 17
/
DEQueue.c
96 lines (84 loc) · 1.76 KB
/
DEQueue.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
/*
Author Fakhra Najm
Email fnajm09@gmail.com
***********************
* Implementation of DEQueue using array
*/
#include<stdio.h>
#include<stdlib.h>
struct Queue{
int size;
int front;
int rear;
int *items;
};
void createQ(struct Queue *queue){
queue->size = 10;
queue->front = -1;
queue->rear = -1;
queue->items = (int *)malloc(queue->size*sizeof(int));
}
int isEmpty(struct Queue *queue){
if(queue->front == queue->rear)
return 1;
return 0;
}
int isFull(struct Queue *queue){
if(queue->rear == queue->size-1)
return 1;
return 0;
}
void enqueueFront(struct Queue *queue, int value){
if(isFull(queue))
printf("Queue is full\n");
else if(queue->front == -1){
queue->front ++;
queue->items[queue->front] = value;
}
else{
queue->items[queue->front] = value;
queue->front --;
}
}
void enqueueRear(struct Queue *queue, int value){
if(isFull(queue))
printf("Queue is full\n");
else{
queue->rear ++;
queue->items[queue->rear] = value;
}
}
int dequeueFront(struct Queue *queue){
int value = -1;
if(isEmpty(queue))
return value;
queue->front ++;
value = queue->items[queue->front];
return value;
}
int dequeueRear(struct Queue *queue){
if(isEmpty(queue))
return -1;
int value = queue->items[queue->rear];
queue->rear --;
return value;
}
int main(){
struct Queue queue;
createQ(&queue);
enqueueRear(&queue, 10);
enqueueRear(&queue, 20);
enqueueRear(&queue, 30);
enqueueRear(&queue, 40);
enqueueRear(&queue, 50);
printf("%d ",dequeueFront(&queue));
printf("%d ",dequeueRear(&queue));
printf("%d ",dequeueRear(&queue));
printf("%d ",dequeueRear(&queue));
printf("%d ",dequeueRear(&queue));
enqueueFront(&queue, 60);
printf("%d\n",dequeueFront(&queue));
enqueueFront(&queue, 70);
printf("%d\n",dequeueFront(&queue));
return 0;
}