-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
128 lines (93 loc) · 3.52 KB
/
queue.c
File metadata and controls
128 lines (93 loc) · 3.52 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/** File queue.c
*
* @author Nathaniel Miller
*
* Functions which operate on queues:
* create a queue, add an entry to the queue,
* and remove an entry from the queue.
*/
#include "queue.h"
/** Creates a queue by allocating a Queue struct,
* then initializes it, and allocates memory to hold
* the entries to the queue.
*
* @param size The maximum number of entries in the queue.
* @return new_queue A pointer to the newly created Queue struct.
*/
Queue *create_queue(int size) {
Queue *new_queue; /* Hold pointer to the created Queue struct */
new_queue = (Queue *) malloc(sizeof(Queue));
if (new_queue == NULL) return NULL; /* if unable to allocate space */
new_queue->size = size; /* fill in the struct field */
/* allocate memory for the queue entries */
new_queue->contents = (void **) calloc(sizeof(void *), size);
if(new_queue->contents == NULL) {
free(new_queue); /* free struct if unable to allocat entries space */
return NULL;
} /* end if */
new_queue->queue_tail = new_queue->contents; /* start at tail of queue */
new_queue->queue_head = NULL; /* make head NULL */
return new_queue; /* return a pointer to the newly-created Queue struct */
} /* end function create_queue */
/** Adds an entry onto the tail-end of the Queue.
* Tail pointer is increased after entry is added.
*
* @param *new_q The Queue struct.
* @param *entry Pointer to an entry to be added to the Queue.
*/
int enqueue(Queue *new_q, void *entry) {
/* checks if queue is full */
if(new_q->queue_head == new_q->queue_tail) {
return -1; /* return if full */
} /* end if */
/* check if queue is empty */
if(new_q->queue_head == NULL) {
new_q->queue_head = new_q->queue_tail; /* set head as tail, if empty */
} /* end if */
/* check if the tail is at the end of the queue */
if(new_q->queue_tail == (new_q->contents + ((new_q->size) - 1))) {
*(new_q->queue_tail) = entry; /* add queue entry onto the queue */
new_q->queue_tail = new_q->contents; /* set the tail to the base */
} /* end if */
else {
*(new_q->queue_tail) = entry; /* add queue entry onto the queue */
(new_q->queue_tail)++; /* advance the tail location */
} /* end else */
return 0; /* success */
} /* end function enqueue */
/** Removes an entry from the Queue.
* Entry is removed from the head-end.
* Head pointer is increased to next entry.
*
* @param *new_q The Queue struct.
* @return ent The stored removed value.
*/
void* dequeue(Queue *new_q) {
/* check if queue is empty */
if(new_q->queue_head == NULL) {
printf("Queue is empty. Cannot remove an entry from an empty Queue!");
return NULL;
} /* end if */
void* ent = *(new_q->queue_head); /* store the removed entry */
/* check if head is at the end of the queue */
if(new_q->queue_head == (new_q->contents + ((new_q->size) - 1))) {
new_q->queue_head = new_q->contents; /* set the head as the base */
} /* end inner if */
else {
(new_q->queue_head)++; /* otherwise advance the head */
} /* end else */
/* check if the queue is empty now */
if(new_q->queue_head == new_q->queue_tail) {
new_q->queue_head = NULL;
} /* end if */
return ent; /* return the stored entry */
} /* end function dequeue */
/** Deletes the Queue, freeing any
* allocated memory or structures.
*
* @param *the_q The Queue to be deleted.
*/
void delete_queue(Queue *the_q) {
free(the_q->contents); /* Free the memory holding the queue entries */
free(the_q); /* free the queue struct */
} /* end function delete_queue */