-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
137 lines (121 loc) · 3.63 KB
/
queue.c
File metadata and controls
137 lines (121 loc) · 3.63 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
129
130
131
132
133
134
135
136
137
/*
* This file is where you should implement your queue. It already contains
* skeletons of the functions you need to implement (along with documentation
* for each function). Feel free to implement any additional functions you
* might need. Also, don't forget to include your name and @oregonstate.edu
* email address below.
*
* Name:
* Email:
*/
#include <stdlib.h>
#include <assert.h>
#include "queue.h"
#include "dynarray.h"
/*
* This is the structure that will be used to represent a queue. This
* structure specifically contains a single field representing a dynamic array
* that should be used as the underlying data storage for the queue.
*
* You should not modify this structure.
*/
struct queue {
struct dynarray* array;
};
/*
* This function should allocate and initialize a new, empty queue and return
* a pointer to it.
*/
struct queue* queue_create() {
struct queue* queue = malloc(sizeof(struct queue));
queue->array = dynarray_create();
return queue;
}
/*
* This function should free the memory associated with a queue. While this
* function should up all memory used in the queue itself, it should not free
* any memory allocated to the pointer values stored in the queue. This is the
* responsibility of the caller.
*
* Params:
* queue - the queue to be destroyed. May not be NULL.
*/
void queue_free(struct queue* queue) {
dynarray_free(queue->array);
free(queue);
return;
}
/*
* This function should indicate whether a given queue is currently empty.
* Specifically, it should return 1 if the specified queue is empty (i.e.
* contains no elements) and 0 otherwise.
*
* Params:
* queue - the queue whose emptiness is being questioned. May not be NULL.
*/
int queue_isempty(struct queue* queue) {
if(dynarray_size(queue->array) == 0){
return 1;
}
return 0;
}
/*
* This function should enqueue a new value into a given queue. The value to
* be enqueued is specified as a void pointer. This function must have O(1)
* average runtime complexity.
*
* Params:
* queue - the queue into which a value is to be enqueued. May not be NULL.
* val - the value to be enqueued. Note that this parameter has type void*,
* which means that a pointer of any type can be passed.
*/
void queue_enqueue(struct queue* queue, void* val) {
dynarray_enqueue(queue->array, val);
return;
}
/*
* This function should return the value stored at the front of a given queue
* *without* removing that value. This function must have O(1) average runtime
* complexity.
*
* Params:
* queue - the queue from which to query the front value. May not be NULL.
*
* Return:
* This function should return the front value in the queue.
*/
void* queue_front(struct queue* queue) {
void* val = dynarray_get_front(queue->array);
return val;
}
/*
* This function should dequeue a value from a given queue and return the
* dequeued value. This function must have O(1) average runtime complexity.
*
* Params:
* queue - the queue from which a value is to be dequeued. May not be NULL.
*
* Return:
* This function should return the value that was dequeued.
*/
void* queue_dequeue(struct queue* queue) {
void* val = dynarray_dequeue(queue->array);
return val;
}
/*
* helper function for testing, do not modify
*/
/*
* This function print out the queue. (for testing circular buffer)
* Params:
* queue - the queue from which a value is to be print. May not be NULL.
* void (*p) (void* a) - a function pointer to print an element within the queue
* Return:
* none.
*/
void queue_print(struct queue* queue, void (*p) (void* a)){
if (queue == NULL)
return;
dynarray_print(queue->array, p);
return;
}