-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqlib_static.c
More file actions
59 lines (55 loc) · 1.27 KB
/
Copy pathqlib_static.c
File metadata and controls
59 lines (55 loc) · 1.27 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
/*
* qlib.c Matt Bishop April 4, 2005
*
* Implement a "queue of integers" module.
*/
#include <stdio.h>
#include <stdlib.h>
#include "fqlib_static.h"
/*
* create or delete a queue
*
* PARAMETERS: QUEUE **qptr space for, or pointer to, queue
* int flag 1 for create, 0 for delete
* int size max elements in queue
*/
void qmanage(QUEUE **qptr, int flag, int size)
{
if (flag){
/* allocate a new queue */
*qptr = malloc(sizeof(QUEUE));
(*qptr)->head = (*qptr)->count = 0;
(*qptr)->que = malloc(size * sizeof(int));
(*qptr)->size = size;
}
else{
/* delete the current queue */
(void) free((*qptr)->que);
(void) free(*qptr);
}
}
/*
* add an element to an existing queue
*
* PARAMETERS: QUEUE *qptr pointer for queue involved
* int n element to be appended
*/
void put_on_queue(QUEUE *qptr, int n)
{
/* add new element to tail of queue */
qptr->que[(qptr->head + qptr->count) % qptr->size] = n;
qptr->count++;
}
/*
* take an element off the front of an existing queue
*
* PARAMETERS: QUEUE *qptr pointer for queue involved
* int *n storage for the return element
*/
void take_off_queue(QUEUE *qptr, int *n)
{
/* return the element at the head of the queue */
*n = qptr->que[qptr->head++];
qptr->count--;
qptr->head %= qptr->size;
}