-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathQueueArray.c
89 lines (77 loc) · 1.44 KB
/
QueueArray.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
/*
* Author Fakhra Najm
* EMail fnajm09@gmail.com
Implementation of Queue using Array
* 1. createQ
* 2. enqueue
* 3. dequeue
* 4. printQ
*/
#include<stdio.h>
#include<stdlib.h>
struct Queue
{
int size;
int front; // index pointer to front end
int rear; // index pointer to rear end
int *items;
};
/*
* creates and initialize the members of queue
* @param q pointer to the Queue
*/
void createQ(struct Queue *q){
printf("Enter size of the queue:\n");
scanf("%d", &q->size);
q->front = q->rear = -1;
q->items = (int *)malloc(q->size*sizeof(int));
}
/*
* inserts the passed value in the queue
* @param q pointer to the queue
* @param value to be inserted
*/
void enqueue(struct Queue *q, int value){
if(q->rear == q->size-1)
printf("Queue is full\n");
else{
q->rear ++;
q->items[q->rear] = value;
}
}
/*
* deletes the front element from queue
* @param q pointer to the Queue
* @return deleted value
*/
int dequeue(struct Queue *q){
int value = -1;
if(q->front == q->rear)
printf("Queue is empty\n");
else{
q->front ++;
value = q->items[q->front];
}
return value;
}
/*
* prints the elements of Queue
*/
void printQ(struct Queue q){
for (int i = q.front+1; i <= q.rear; ++i)
printf("%d ", q.items[i]);
printf("\n");
}
int main(){
struct Queue q;
createQ(&q);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
enqueue(&q, 40);
enqueue(&q, 50);
printQ(q);
printf("%d\n",dequeue(&q));
printQ(q);
return 0;
}