From f28846ca7973f62ee870388385ccbf944d66d044 Mon Sep 17 00:00:00 2001 From: Valentin Gehrke Date: Fri, 12 Feb 2016 10:06:30 +0100 Subject: [PATCH] EPA: Linked stack --- linkedstack.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 linkedstack.c diff --git a/linkedstack.c b/linkedstack.c new file mode 100644 index 0000000..00a9ec0 --- /dev/null +++ b/linkedstack.c @@ -0,0 +1,49 @@ +#include +#include +#include + +typedef struct stacknode { + int element; + struct stacknode* next; +} Node, **Stack; + +Stack sCreate(); +void sPush(Stack s, int element); +int sPop(Stack s); +int sEmpty(Stack s); + +Stack sCreate() { + Stack s = malloc(sizeof(Node*)); + *s = NULL; + return s; +} + +void sPush(Stack s, int element) { + Node* n = malloc(sizeof(Node)); + n->element = element; + n->next = *s; + *s = n; +} + +int sPop(Stack s) { + assert(!sEmpty(s)); + Node* tmp = *s; + *s = tmp->next; + int element = tmp->element; + free(tmp); + return element; +} + +int sEmpty(Stack s) { + return (*s == NULL); +} + +int main( int argc, char *argv[] ) { + Stack s = sCreate(); + + for(int i = 0; i < 10; i++) sPush(s,i); + + while(!sEmpty(s)) printf("%d\n",sPop(s)); + + return 0; +}