-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.c
More file actions
45 lines (35 loc) · 789 Bytes
/
stack.c
File metadata and controls
45 lines (35 loc) · 789 Bytes
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
typedef struct stack{
int data[MAX];
int top;
} stack_t;
stack_t *init() {
stack_t *stack;
stack = (stack_t*) malloc(sizeof(stack_t));
if (stack == NULL) exit(EXIT_FAILURE);
stack->top = 0;
return stack;
}
void push(stack_t *stack, int value) {
stack->data[stack->top] = value;
stack->top++;
}
void pop(stack_t *stack) {
if (stack->top == 0) exit(EXIT_FAILURE);
stack->top--;
stack->data[stack->top] = 0;
}
int peek(stack_t *stack) {
return stack->data[stack->top - 1];
}
void getAll(stack_t *stack) {
for (int i = 0; i < stack->top; i++) {
printf("%d\n", stack->data[i]);
}
}
void free_stack(stack_t *stack) {
free(stack->data);
free(stack);
}