EPA: Linked stack
This commit is contained in:
parent
aa9377d5fc
commit
f28846ca79
1 changed files with 49 additions and 0 deletions
49
linkedstack.c
Normal file
49
linkedstack.c
Normal 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;
|
||||
}
|
Loading…
Reference in a new issue