Skip to content

Commit 5f3e264

Browse files
committed
update clang queue
1 parent 8adaa39 commit 5f3e264

File tree

4 files changed

+98
-7
lines changed

4 files changed

+98
-7
lines changed

c/queue/queue.c

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,66 @@ Queue *queueCreate() {
1010
Queue *q = malloc(sizeof(Queue));
1111
assert(q);
1212
q->theSize = 0;
13-
q->capacity = 0;
14-
q->dataStore
13+
q->capacity = DEFAULT_QUEUE_SIZE;
14+
q->dataStore = malloc(sizeof(void *) * DEFAULT_QUEUE_SIZE);
1515

16+
q->enqueue = queueEnqueue;
17+
q->dequeue = queueDequeue;
18+
q->size = queueSize;
19+
q->isEmpty = queueIsEmpty;
20+
q->font = queueFont;
21+
q->end = queueEnd;
22+
q->destory = queueDestory;
1623
}
1724

25+
void queueShouldRealloc(Queue *q) {
26+
if (q->theSize == q->capacity) {
27+
q->capacity = q->capacity * 2;
28+
q->dataStore = realloc(q->dataStore, sizeof(void *) * q->capacity);
29+
}
30+
}
31+
32+
void queueEnqueue(Queue *q, void *element) {
33+
queueShouldRealloc(q);
34+
q->dataStore[q->theSize++] = element;
35+
}
36+
37+
void* queueDequeue(Queue *q) {
38+
if (q->theSize == 0) return NULL;
39+
void *d = q->dataStore[0];
40+
if (q->theSize > 1) {
41+
int i;
42+
for (i = 0; i < q->theSize-1; i++) {
43+
q->dataStore[i] = q->dataStore[i+1];
44+
}
45+
}
46+
q->theSize--;
47+
return d;
48+
}
49+
50+
int queueSize(Queue *q) {
51+
return q->theSize;
52+
}
53+
54+
bool queueIsEmpty(Queue *q) {
55+
return q->theSize == 0;
56+
}
1857

58+
void *queueFont(Queue *q) {
59+
if (q->theSize == 0) return NULL;
60+
return q->dataStore[0];
61+
}
62+
63+
void *queueEnd(Queue *q) {
64+
if (q->theSize == 0) return NULL;
65+
return q->dataStore[q->theSize-1];
66+
}
67+
68+
void queueClear(Queue *q) {
69+
q->theSize = 0;
70+
}
71+
72+
void queueDestory(Queue *q) {
73+
free(q->dataStore);
74+
free(q);
75+
}

c/queue/queue.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ typedef struct Queue {
1010
void *(*dequeue)();
1111
int (*size)();
1212
bool (*isEmpty)();
13-
void *(font)();
14-
void *(end)();
15-
void (clear)();
13+
void *(*font)();
14+
void *(*end)();
15+
void (*clear)();
16+
void (*destory)();
1617
} Queue;
1718

1819
/* Prototypes */
@@ -24,5 +25,6 @@ bool queueIsEmpty(Queue *);
2425
void *queueFont(Queue *);
2526
void *queueEnd(Queue *);
2627
void queueClear(Queue *);
28+
void queueDestory(Queue *);
2729

28-
#endif __QUEUE_H__
30+
#endif /*__QUEUE_H__*/

c/queue/queue_test.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <assert.h>
2+
#include <stdio.h>
3+
#include "queue.c"
4+
5+
int main() {
6+
printf("\nRunning queue.c tests.\n\n");
7+
8+
Queue *q = queueCreate();
9+
assert(q->isEmpty(q));
10+
assert(q->size(q) == 0);
11+
12+
int n1 = 1, n2 = 2, n3 = 3;
13+
int *p1 = &n1, *p2 = &n2, *p3 = &n3;
14+
q->enqueue(q, p1);
15+
q->enqueue(q, p2);
16+
q->enqueue(q, p3);
17+
assert(q->size(q) == 3);
18+
19+
int d = *(int*)(q->dequeue(q));
20+
assert(q->size(q) == 2);
21+
assert(d == 1);
22+
23+
int d2 = *(int*)q->font(q);
24+
int d3 = *(int*)q->end(q);
25+
assert(d2 == 2);
26+
assert(d3 == 3);
27+
28+
q->destory(q);
29+
30+
printf("All queue.c tests completed.\n\n");
31+
return 0;
32+
}

c/stack/stack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Stack *stackCreate() {
2828
void stackShouldRealloc(Stack *s) {
2929
if (s->top == s->capacity) {
3030
s->capacity = s->capacity*2;
31-
s->dataStore = realloc(s->dataStore, sizeof(void *)*s->capacity);
31+
s->dataStore = realloc(s->dataStore, sizeof(void *) * s->capacity);
3232
}
3333
}
3434

0 commit comments

Comments
 (0)