Skip to content

Commit baf29e3

Browse files
committed
update clang stack
1 parent 57ef89c commit baf29e3

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

c/stack/stack.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include <assert.h>
4+
#include <stdbool.h>
5+
#include "stack.h"
6+
7+
#define DEFAULT_STACK_SIZE 4
8+
9+
/**
10+
* create a new empty stack
11+
*/
12+
Stack *stackCreate() {
13+
Stack *stack = malloc(sizeof(Stack));
14+
assert(stack);
15+
stack->top = 0;
16+
stack->capacity = DEFAULT_STACK_SIZE;
17+
stack->dataStore = malloc(sizeof(void *)*DEFAULT_STACK_SIZE);
18+
19+
stack->size = stackSize;
20+
stack->isEmpty = stackIsEmpty;
21+
stack->push = stackPush;
22+
stack->pop = stackPop;
23+
stack->peek = stackPeek;
24+
stack->clear = stackClear;
25+
stack->destory = stackDestory;
26+
}
27+
28+
void stackShouldRealloc(Stack *s) {
29+
if (s->top == s->capacity) {
30+
s->capacity = s->capacity*2;
31+
s->dataStore = realloc(s->dataStore, sizeof(void *)*s->capacity);
32+
}
33+
}
34+
35+
int stackSize(Stack *s) {
36+
return s->top;
37+
}
38+
39+
bool stackIsEmpty(Stack *s) {
40+
return s->top == 0;
41+
}
42+
43+
void stackPush(Stack *s, void *element) {
44+
stackShouldRealloc(s);
45+
s->dataStore[s->top++] = element;
46+
}
47+
48+
void *stackPop(Stack *s) {
49+
if (s->isEmpty(s)) {
50+
return NULL;
51+
}
52+
53+
return s->dataStore[--s->top];
54+
}
55+
56+
void *stackPeek(Stack *s) {
57+
if (s->isEmpty(s)) {
58+
return NULL;
59+
}
60+
61+
return s->dataStore[s->top-1];
62+
}
63+
64+
void stackClear(Stack *s) {
65+
s->top = 0;
66+
}
67+
68+
void stackDestory(Stack *s) {
69+
free(s->dataStore);
70+
free(s);
71+
}

c/stack/stack.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef __STACK_H__
2+
#define __STACK_H__
3+
4+
typedef struct Stack {
5+
int top;
6+
int capacity;
7+
void **dataStore;
8+
9+
int (*size)();
10+
bool (*isEmpty)();
11+
void (*push)();
12+
void *(*pop)();
13+
void *(*peek)();
14+
void (*clear)();
15+
void (*destory)();
16+
} Stack;
17+
18+
/* Prototypes */
19+
Stack *stackCreate();
20+
int stackSize(Stack *s);
21+
bool stackIsEmpty(Stack *s);
22+
void stackPush(Stack *s, void *);
23+
void *stackPop(Stack *s);
24+
void *stackPeek(Stack *s);
25+
void stackClear(Stack *s);
26+
void stackDestory(Stack *s);
27+
28+
#endif /* __STACK_H__ */

c/stack/stack_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 "stack.c"
4+
5+
int main() {
6+
printf("\nRunning stack.c tests.\n\n");
7+
8+
Stack *s = stackCreate();
9+
assert(s->isEmpty(s));
10+
assert(s->size(s) == 0);
11+
12+
char *c1 = "12";
13+
char *c2 = "23";
14+
char *c3 = "34";
15+
s->push(s, c1);
16+
s->push(s, c2);
17+
s->push(s, c3);
18+
assert(s->size(s) == 3);
19+
20+
char *c4 = "34";
21+
assert(strcmp(c4, (char *)s->pop(s)) == 0);
22+
char *c5 = "23";
23+
assert(strcmp(c5, (char *)s->peek(s)) == 0);
24+
25+
s->clear(s);
26+
assert(s->isEmpty(s));
27+
28+
s->destory(s);
29+
30+
printf("All stack.c tests completed.\n\n");
31+
return 0;
32+
}

0 commit comments

Comments
 (0)