1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
|
#include <stdio.h>
#include <stdlib.h>
struct entry {
int value;
struct entry* prev;
};
struct entry* push(struct entry* top, int value) {
struct entry* newEntry = (struct entry*)malloc(sizeof(struct entry));
newEntry->value = value;
newEntry->prev = top;
top = newEntry;
return newEntry;
}
int pop(struct entry** top) {
int value = (*top)->value;
*top = (*top)->prev;
return value;
}
int main() {
struct entry* stack = (struct entry*)malloc(sizeof(struct entry));
stack = push(stack, 1);
stack = push(stack, 2);
stack = push(stack, 3);
printf("%d\n", pop(&stack));
printf("%d\n", pop(&stack));
printf("%d\n", pop(&stack));
} |