-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
72 lines (64 loc) · 1.43 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
#include "queue.h"
#include <stdio.h>
#include <stdlib.h>
queue_t *create_queue() // return a newly created empty queue
{
// DO NOT MODIFY!!!
queue_t *Q = (queue_t *)malloc(sizeof(queue_t));
Q->list = create_list();
Q->front = Q->list->head;
Q->rear = Q->list->tail;
Q->size = Q->list->size;
return Q;
}
void enqueue(queue_t *q, int data) // TODO: insert data at the end of a queue
{
node_t *t = malloc(sizeof(node_t));
t->data = data;
t->next = t->prev = NULL;
if (empty(q))
{
q->front = t;
t->prev = q->front;
q->rear = q->front;
}
else
{
q->rear->next = t;
t->prev = q->rear;
q->rear = q->rear->next;
}
q->size++;
}
int dequeue(queue_t *q) // TODO: return the data at the front of a queue and remove it. Return -1 if queue is empty
{
int x;
if (empty(q))
return (-1);
node_t *first = q->front;
q->front = first->next;
free(first);
q->size--;
}
int front(queue_t *q) // TODO: return the data at the front of a queue. Return -1 if queue is empty
{
if (is_empty)
return (-1);
return (q->front->data);
}
int empty(queue_t *q) // return if the queue is empty
{
// DO NOT MODIFY!!!
return is_empty(q->list);
}
int queue_size(queue_t *q) // returns the number of elements in the queue
{
// DO NOT MODIFY!!!
return q->size;
}
void delete_queue(queue_t *q) // empty the contents of the queue
{
// DO NOT MODIFY!!!
delete_list(q->list);
free(q);
}