-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
85 lines (80 loc) · 1.48 KB
/
queue.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "queue.h"
#define Q_SIZE 860000
queue *createQueue()
{
queue *q = (queue *)malloc(sizeof(queue *));
q->size = Q_SIZE;
q->items = malloc(sizeof(q->items) * q->size);
q->front = -1;
q->rear = -1;
return q;
}
void resize(queue *q)
{
q->size = q->size * 2;
q->items = realloc(q->items, sizeof(q->items) * q->size);
printf("Resizing to size: %i\n", q->size);
}
int dequeue(queue *q)
{
int item;
if (isEmpty(q))
{
printf("Queue is empty\n");
item = -1;
}
else
{
item = q->items[q->front];
q->front++;
if (q->front > q->rear)
{
printf("Resetting queue\n");
q->front = q->rear = -1;
}
}
return item;
}
int isEmpty(queue *q)
{
if (q->rear == -1)
return 1;
else
return 0;
}
void checkForResize(queue *q)
{
}
void enqueue(queue *q, int value)
{
// TODO remake for automatic rezise
if (q->rear == q->size - 1)
{
resize(q);
}
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
}
void display(queue *q) {}
void printQueue(queue *q)
{
int i = q->front;
if (isEmpty(q))
{
printf("Queue is empty");
}
else
{
printf("Queue contains ");
for (i = q->front; i < q->rear + 1; i++)
{
printf("%d ", q->items[i]);
}
printf("\n");
}
}