-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDEqueue.c
More file actions
103 lines (98 loc) · 1.74 KB
/
DEqueue.c
File metadata and controls
103 lines (98 loc) · 1.74 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
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
#include <stdio.h>
#include <stdlib.h>
struct queue
{
int size;
int front;
int rear;
int *Q;
};
void create(struct queue *q, int size);
void rear_enqueue(struct queue *q, int x);
int rear_dequeue(struct queue *q);
void front_enqueue(struct queue *q, int x);
int front_dequeue(struct queue *q);
void display(struct queue q);
int main()
{
struct queue q;
create(&q, 4);
rear_enqueue(&q, 1);
rear_enqueue(&q, 2);
rear_enqueue(&q, 3);
front_dequeue(&q);
front_dequeue(&q);
front_enqueue(&q, 21);
front_enqueue(&q, 21);
rear_dequeue(&q);
printf(" %d \n ", q.front);
printf(" %d \n ", q.rear);
display(q);
}
void create(struct queue *q, int size)
{
q->size = size;
q->front = q->rear = -1;
q->Q = (int *)malloc(q->size * sizeof(int));
}
void rear_enqueue(struct queue *q, int x)
{
if (q->rear == q->size - 1)
{
printf("queue is full");
}
else
{
q->rear++;
q->Q[q->rear] = x;
}
}
int rear_dequeue(struct queue *q)
{
int x = -1;
if (q->rear == q->front)
{
printf("queue is empty ");
}
else
{
x = q->Q[q->rear];
q->rear--;
}
return x;
}
void front_enqueue(struct queue *q, int x)
{
if (q->front == -1)
{
printf("en_queue is not possible\n");
}
else
{
q->Q[q->front] = x;
q->front--;
}
}
int front_dequeue(struct queue *q)
{
int x = -1;
if (q->rear == q->front)
{
printf("queue is empty ");
}
else
{
q->front++;
x = q->Q[q->front];
}
return x;
}
void display(struct queue q)
{
int i;
for (i = q.front + 1; i <= q.rear; i++)
{
printf("%d ", q.Q[i]);
}
printf("\n");
}