-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathstack.c
More file actions
54 lines (46 loc) · 996 Bytes
/
Copy pathstack.c
File metadata and controls
54 lines (46 loc) · 996 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
46
47
48
49
50
51
52
53
54
#include "stack.h"
#include <stdlib.h>
#define length(arr) (sizeof (arr) / sizeof *(arr))
struct Stack
{
int arr[10];
int top;
};
Stack *create(void)
{
Stack *stack = malloc(sizeof(Stack));
if (!stack)
return NULL;
stack->top = -1;
return stack;
}
inline static _Bool overflow(Stack *stack)
{
return length(stack->arr) - 1 == stack->top;
}
inline static _Bool underflow(Stack *stack)
{
return -1 == stack->top;
}
int printf(const char *, ...);
void push(Stack *stack, int data)
{
if (overflow(stack))
printf("Stack overflow!\n");
else
stack->arr[++(stack->top)] = data;
}
void pop(Stack *stack)
{
if (underflow(stack))
printf("Stack underflow!\n");
else
printf("%d\n", stack->arr[(stack->top)--]);
}
void top(Stack *stack)
{
if (underflow(stack))
printf("Stack underflow!\n");
else
printf("%d\n", stack->arr[stack->top]);
}