-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayQueue.c
More file actions
84 lines (68 loc) · 2.38 KB
/
ArrayQueue.c
File metadata and controls
84 lines (68 loc) · 2.38 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
//
// Created by Vincent on 2021/5/16.
//
#include "ArrayQueue.h"
#include <stdio.h>
#include <stdlib.h>
void arrayQueuePrintf(ArrayQueue *arrayQueue) {
int headIndex = arrayQueue->head;
int tailIndex = arrayQueue->tail;
if (headIndex == tailIndex) {
printf("SeqQueue is empty \n");
return;
}
printf("queue content is: ");
ArrayQueueElement *elementArr = arrayQueue->elementArr;
char *(*toStringElement)(void *) =arrayQueue->toStringElement;
int capacity = arrayQueue->capacity;
for (int i = headIndex; i != tailIndex; i++) {
i %= capacity;
printf("%s\t", toStringElement(elementArr[i].dataPointer));
}
printf("\n");
}
ArrayQueue *arrayQueueInitiate(int capacity, bool (*equalsElement)(void *, void *),
char *(*toStringElement)(void *)) {
if (capacity < 0) {
printf("Illegal Capacity: %d", capacity);
exit(1);
}
ArrayQueue *queue = malloc(sizeof(ArrayQueue));
queue->elementArr = malloc(sizeof(ArrayQueueElement) * capacity);
queue->capacity = capacity;
queue->head = 0;
queue->tail = 0;
queue->equalsElement = equalsElement;
queue->toStringElement = toStringElement;
return queue;
}
bool arrayQueueIsEmpty(ArrayQueue *arrayQueue) { return arrayQueue->head == arrayQueue->tail; }
void arrayQueueEnElement(ArrayQueue *arrayQueue, void *dataPointer) {
int headIndex = arrayQueue->head;
int tailIndex = arrayQueue->tail;
int capacity = arrayQueue->capacity;
if ((tailIndex + 1) % capacity == headIndex) {
printf("队列已满 \n");
exit(1);
}
ArrayQueueElement *elementArr = arrayQueue->elementArr;
elementArr[(arrayQueue->tail)++].dataPointer = dataPointer;
}
void *arrayQueueOutElement(ArrayQueue *arrayQueue) {
if (arrayQueueIsEmpty(arrayQueue)) {
printf("队列已空 \n");
exit(1);
}
ArrayQueueElement *elementArr = arrayQueue->elementArr;
ArrayQueueElement headElement = elementArr[arrayQueue->head];
arrayQueue->head = (arrayQueue->head + 1) % arrayQueue->capacity;
return headElement.dataPointer;
}
void *arrayQueueGetHeadElement(ArrayQueue *arrayQueue) {
if (arrayQueueIsEmpty(arrayQueue)) {
printf("队列已空 \n");
exit(1);
}
ArrayQueueElement *elementArr = arrayQueue->elementArr;
return elementArr[arrayQueue->head].dataPointer;
}