50 lines
828 B
C
50 lines
828 B
C
|
#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;
|
||
|
}
|