EPA: Linked stack

This commit is contained in:
madmaurice 2016-02-12 10:06:30 +01:00
parent aa9377d5fc
commit f28846ca79

49
linkedstack.c Normal file
View file

@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
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;
}