49 lines
779 B
C
49 lines
779 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
typedef struct Node node;
|
|
struct Node {
|
|
int id;
|
|
struct Node *next;
|
|
};
|
|
|
|
int peek(node *stack) { return stack->id; }
|
|
|
|
void push(node **stack, int id) {
|
|
node *new = malloc(sizeof(node));
|
|
new->id = id;
|
|
|
|
new->next = *stack;
|
|
*stack = new;
|
|
}
|
|
|
|
int pop(node **stack) {
|
|
int id = (*stack)->id;
|
|
node *next = (*stack)->next;
|
|
free(*stack);
|
|
*stack = next;
|
|
return id;
|
|
}
|
|
|
|
void traverse(node *stack) {
|
|
while (stack != NULL) {
|
|
printf("%d ", stack->id);
|
|
stack = stack->next;
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
int main() {
|
|
node *stack = NULL;
|
|
push(&stack, 4);
|
|
push(&stack, 99);
|
|
push(&stack, 23);
|
|
printf("Pop: %d\n", pop(&stack));
|
|
printf("Traverseal: ");
|
|
traverse(stack);
|
|
|
|
// free
|
|
while (stack != NULL)
|
|
pop(&stack);
|
|
}
|