From: Michael Göhler Date: Sun, 7 Sep 2014 19:38:59 +0000 (+0200) Subject: added a char stack implementation for later use X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=584bba9e9357ff7adc0fcedf75fb098c359abb33;p=smdp.git added a char stack implementation for later use --- diff --git a/Makefile b/Makefile index 442ddc4..c56f247 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CFLAGS=-g -Wall -tmp: tmp.c cstring.o markdown.o parser.o viewer.o - cc $(CFLAGS) -o tmp tmp.c cstring.o markdown.o parser.o viewer.o -lncurses +tmp: tmp.c cstring.o cstack.o markdown.o parser.o viewer.o + cc $(CFLAGS) -o tmp tmp.c cstring.o cstack.o markdown.o parser.o viewer.o -lncurses viewer.o: viewer.c cc $(CFLAGS) -c viewer.c @@ -12,16 +12,19 @@ parser.o: parser.c markdown.o: markdown.c cc $(CFLAGS) -c markdown.c +cstack.o: cstack.c + cc $(CFLAGS) -c cstack.c + cstring.o: cstring.c cc $(CFLAGS) -c cstring.c all: tmp +clean: + rm -f tmp *.o + .PHONY: test test: $(MAKE) -C test -clean: - rm -f tmp *.o - diff --git a/cstack.c b/cstack.c new file mode 100644 index 0000000..6474436 --- /dev/null +++ b/cstack.c @@ -0,0 +1,44 @@ +#include // malloc, realloc + +#include "include/cstack.h" + +cstack_t *cstack_init() { + cstack_t *stack = malloc(sizeof(cstack_t)); + stack->content = (void*)0; + stack->alloc = stack->size = 0; + stack->head = -1; + stack->push = cstack_push; + stack->pop = cstack_pop; + stack->top = cstack_top; + stack->empty = cstack_empty; + stack->delete = cstack_delete; + return stack; +} + +void cstack_push(cstack_t *self, char c) { + if(self->size + sizeof(c) > self->alloc) { + self->alloc += (sizeof(char)); + self->content = realloc(self->content, self->alloc); + } + self->content[++self->head] = c; + self->size += (sizeof(char)); +} + +char cstack_pop(cstack_t *self) { + self->size -= (sizeof(char)); + return self->content[self->head--]; +} + +char cstack_top(cstack_t *self) { + return self->content[self->head]; +} + +int cstack_empty(cstack_t *self) { + return self->head == -1; +} + +void cstack_delete(cstack_t *self) { + free(self->content); + free(self); +} + diff --git a/include/cstack.h b/include/cstack.h new file mode 100644 index 0000000..edb9847 --- /dev/null +++ b/include/cstack.h @@ -0,0 +1,22 @@ +#if !defined( CSTACK_H ) +#define CSTACK_H + +typedef struct _cstack_t { + char *content; + size_t alloc; + size_t size; + int head; + void (*push)(struct _cstack_t *self, char c); + char (*pop)(struct _cstack_t *self); + char (*top)(struct _cstack_t *self); + int (*empty)(struct _cstack_t *self); + void (*delete)(struct _cstack_t *self); +} cstack_t; + +void cstack_push(cstack_t *self, char c); +char cstack_pop(cstack_t *self); +char cstack_top(cstack_t *self); +int cstack_empty(cstack_t *self); +void cstack_delete(cstack_t *self); + +#endif // !defined( CSTACK_H ) \ No newline at end of file diff --git a/test/cstack.c b/test/cstack.c new file mode 100644 index 0000000..cf1b504 --- /dev/null +++ b/test/cstack.c @@ -0,0 +1,41 @@ +#include + +#include "../include/cstack.h" +#include "../cstack.c" + +int main(int argc, char *argv[]) { + + cstack_t *s = cstack_init(); + printf("INIT: head: %d, size: %zu, alloc: %zu\n", s->head, s->size, s->alloc); + + (s->push)(s, 'a'); + printf("PUSH: char: %c, head: %d, size: %zu, alloc: %zu\n", (s->top)(s), s->head, s->size, s->alloc); + + (s->push)(s, 'b'); + printf("PUSH: char: %c, head: %d, size: %zu, alloc: %zu\n", (s->top)(s), s->head, s->size, s->alloc); + + (s->push)(s, 'c'); + printf("PUSH: char: %c, head: %d, size: %zu, alloc: %zu\n", (s->top)(s), s->head, s->size, s->alloc); + + char ch = (s->pop)(s); + printf("POP: char: %c, head: %d, size: %zu, alloc: %zu\n", ch, s->head, s->size, s->alloc); + + ch = (s->pop)(s); + printf("POP: char: %c, head: %d, size: %zu, alloc: %zu\n", ch, s->head, s->size, s->alloc); + + (s->push)(s, 'd'); + printf("PUSH: char: %c, head: %d, size: %zu, alloc: %zu\n", (s->top)(s), s->head, s->size, s->alloc); + + (s->push)(s, 'e'); + printf("PUSH: char: %c, head: %d, size: %zu, alloc: %zu\n", (s->top)(s), s->head, s->size, s->alloc); + + while(s->size > 0) { + ch = (s->pop)(s); + printf("POP: char: %c, head: %d, size: %zu, alloc: %zu\n", ch, s->head, s->size, s->alloc); + } + + (s->delete)(s); + + return(0); +} +