-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
57 lines (47 loc) · 1.03 KB
/
stack.c
File metadata and controls
57 lines (47 loc) · 1.03 KB
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
//
// stack.c
// PrefixEvalAndTrans
//
// Created by 이재원 on 14/05/2019.
// Copyright © 2019 Jae Won. All rights reserved.
//
#include "stack.h"
#define INIT_STACK_CAPA 10
void allocStack(stack** s){
*s = (stack*)calloc(1, sizeof(stack));
(*s)->stc = (element*)calloc(INIT_STACK_CAPA, sizeof(element));
(*s)->capacity = INIT_STACK_CAPA;
(*s)->top = -1;
}
Boolean isEmpty(stack s){
if(s.top == -1)
return True;
return False;
}
Boolean isFull(stack s){
if(s.top == s.capacity - 1)
return True;
return False;
}
void stackFull(stack* s){
s->capacity *= 2;
s->stc = (element*)realloc(s->stc, s->capacity * sizeof(element));
}
void stackEmpty(){
fprintf(stderr, "ERROR! Stack is empty");
exit(EXIT_FAILURE);
}
void push(stack* s, element e){
if(isFull(*s))
stackFull(s);
s->stc[++(s->top)] = e;
}
element pop(stack* s){
if(isEmpty(*s))
stackEmpty();
return s->stc[(s->top)--];
}
void stackFree(stack** s){
free((*s)->stc);
free(*s);
}