forked from hjsong-pknu/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
76 lines (65 loc) · 1.39 KB
/
stack.c
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#include <string.h>
Stack * create_stack(int initial_size)
{
Stack * s = (Stack *)malloc(sizeof(Stack));
s->contents = (int *)malloc(initial_size*sizeof(Item));
s->top=0;
s->size=initial_size;
return s;
}
void make_empty(Stack * stack)
{
stack->top = 0;
}
bool is_empty(Stack * stack)
{
return stack->top == 0;
}
bool is_full(Stack * stack)
{
return false;
}
void push(Stack * stack, Item i)
{
if (stack->top>=stack->size-1)
reallocate(stack);
stack->contents[stack->top++] = i;
printf("--Pushded: %d\n", i);
}
Item pop(Stack * stack)
{
if (is_empty(stack))
stack_underflow();
else {
printf("--Poped: %d\n", stack->contents[stack->top-1]);
return stack->contents[--stack->top];
}
return '\0'; /* prevents compiler warning due to stack_underflow() call */
}
Item peek(Stack *stack)
{
if (is_empty(stack))
stack_underflow();
else
return stack->contents[stack->top-1];
}
void stack_overflow(void)
{
printf("Expression is too complex\n");
exit(EXIT_FAILURE);
}
void stack_underflow(void)
{
printf("Not enough operands in expression\n");
exit(EXIT_FAILURE);
}
static void reallocate(Stack * stack)
{
int * tmp = (int *)malloc(2*stack->size*sizeof(Item));
memcpy(tmp, stack->contents, stack->size);
free(stack->contents);
stack->contents = tmp;
}