-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
55 lines (48 loc) · 1.24 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
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
/* create_queue
* purpose: create and initialize queue with no items in it
* inputs: none
* output: pointer to the newly created and initialized queue
*/
queue* create_queue()
{
llist *empty_llist = create_llist();
queue *new = NULL;
new = (queue *)malloc(sizeof(queue));
new->st = empty_llist;
return new;
}
/* enqueue
* purpose: insert an item into the queue
* inputs: queue* the queue on which to enqueue the data
* void* the item to enqueue on the queue
* return value: none
*/
void enqueue(queue* q, void* item)
{
insert_head(q->st,item); // here we enq to the beginning
}
/* dequeue
* purpose: return the item that was enqueue least recently
* inputs: queue from which to dequeue the item
* output: item that used to be the top of the queue
*/
void* dequeue(queue* q)
{
if (q->st->head == NULL)
return NULL;
void *thing = peek_tail(q->st);
remove_tail(q->st); // here we deq from the end
return thing;
}
/* queue_is_empty
* purpose: pseudoboolean function that returns whether or not a queue is empty
* inputs: queue
* output: pseudoboolean value - 0 for false, nonzero for true
*/
int queue_is_empty(queue* q)
{
return ((q->st->head == NULL) && (q->st->tail == NULL));
}