-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
68 lines (54 loc) · 1.24 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
#include <stdio.h>
#include <stdlib.h>
struct stack {
struct token *items;
int maxsize;
int top;
};
struct token {
char *token;
char *value;
char* associativity; //left or right
int isOperator;
int precedence;
};
struct stack* newStack(int capacity) {
struct stack *pt = (struct stack*) malloc(sizeof(struct stack));
pt->maxsize = capacity;
pt->top = -1;
pt->items = (struct token*)malloc(sizeof(struct token) * capacity);
return pt;
}
int sizeS(struct stack *pt) {
return pt->top + 1;
}
int isEmptyS(struct stack *pt) {
return pt->top = -1;
}
int isFull(struct stack *pt) {
return pt->top == pt->maxsize - 1;
}
void push(struct stack *pt, struct token x) {
if(isFull(pt)) {
printf("Full\n");
return;
}
printf("Inserting %d\n", x);
pt->items[++pt->top] = x;
}
struct token peek(struct stack *pt) {
if(!isEmptyS(pt)) {
return pt->items[pt->top];
}
else {
printf("Empty\n");
return;
}
}
struct token pop(struct stack *pt) {
if(isEmptyS(pt)) {
printf("Empty\n");
}
printf("Removing %d\n", peek(pt));
return pt->items[pt->top--];
}