-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
79 lines (72 loc) · 1.53 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
77
78
79
/* Group 3
Ashrya Agrawal 2018A7PS0210P
Kalit Naresh Inani 2018A7PS0207P
Prajwal Gupta 2018A7PS0231P */
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
Stack *createStack()
{
Stack *st = (Stack *)malloc(sizeof(Stack));
st->head = NULL;
st->size = 0;
return st;
}
StackNode *createStackNode(TreeNode *tn)
{
StackNode *node = (StackNode *)malloc(sizeof(StackNode));
node->val = tn;
node->nxt = NULL;
return node;
}
void push(Stack *st, TreeNode *tn)
{
StackNode *newNode = createStackNode(tn);
newNode->nxt = st->head;
st->head = newNode;
st->size = st->size + 1;
/*if (tn->isLeaf)
{
printf("PUSHING INTO STACK %s\n", TOKENS[tn->terminal]);
}
else
{
printf("PUSHING INTO STACK %s\n", NONTERMINALS[tn->nonterminal]);
}*/
}
StackNode *pop(Stack *st)
{
if (st->size == 0)
{
printf("Stack is empty.Unable to pop.\n");
return NULL;
}
else
{
StackNode *t = st->head;
st->head = st->head->nxt;
st->size = st->size - 1;
/*if (t->val->isLeaf == 1)
{
printf("POPPING FROM STACK1 %s\n", TOKENS[t->val->terminal]);
}
else
{
printf("POPPING FROM STACK2 %s\n", NONTERMINALS[t->val->nonterminal]);
}*/
return t;
}
}
StackNode *top(Stack *st)
{
if (st->size == 0)
{
printf("Stack is empty.\n");
return NULL;
}
else
{
StackNode *t = st->head;
return t;
}
}