-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.c
More file actions
62 lines (57 loc) · 852 Bytes
/
Queue.c
File metadata and controls
62 lines (57 loc) · 852 Bytes
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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
struct Queue
{
int size;
int front;
int rear;
int* 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 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 dequeue(struct Queue* q)
{
int x = -1;
if (q->front == q->rear)
printf("Queue is Empty\n");
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");
}
int main()
{
struct Queue q;
create(&q, 5);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
Display(q);
printf("%d ", dequeue(&q));
return 0;
}