-
Notifications
You must be signed in to change notification settings - Fork 2
/
stackADT.c
123 lines (109 loc) · 2.13 KB
/
stackADT.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Group Number : 2
1 Dhruv Rawat 2019B3A70537P thedhruvrawat
2 Chirag Gupta 2019B3A70555P Chirag5128
3 Swastik Mantry 2019B1A71019P Swastik-Mantry
4 Shreyas Sheeranali 2019B3A70387P ShreyasSR
5 Vaibhav Prabhu 2019B3A70593P prabhuvaibhav
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "stackADT.h"
/**
* @brief Creates a new stackNode for the given grammar element
*
* @param GE
* @return stackNode*
*/
stackNode *getStackNode(grammarElement *GE)
{
stackNode *node = malloc(sizeof(stackNode));
node->GE = GE;
node->next = NULL;
node->nodeAddr = NULL;
return node;
}
/**
* @brief Allocates memory for the stack from the heap and initializes it
*
* @return stack*
*/
stack *initStack(void)
{
stack *st = malloc(sizeof(stack));
st->top = NULL;
st->size = 0;
return st;
}
/**
* @brief Returns the a pointer to the stackNode to the top of stack
*
* @param st
* @return stackNode*
*/
stackNode *peekStack(stack *st)
{
return st->top;
}
/**
* @brief Pops the stack
*
* @param st
*/
void popStack(stack * st)
{
if (st->size == 0)
{
printf("Stack is empty!\n");
return;
}
stackNode *currTop = st->top;
st->top = st->top->next;
st->size--;
if (st->size == 0) {
free(currTop->GE);
}
free(currTop);
return;
}
/**
* @brief Pushes node onto the stack encapsulating Grammar Element, Address of Parse Node in the Tree
*
* @param st
* @param GE
* @param nodeAddr
*/
void pushStackGE(stack *st, grammarElement *GE, ParseTreeNode* nodeAddr)
{
stackNode *newTop = getStackNode(GE);
newTop->next = st->top;
newTop->nodeAddr = nodeAddr;
st->top = newTop;
st->size++;
return;
}
/**
* @brief Returns if the stack is empty
*
* @param st
* @return true
* @return false
*/
bool isEmpty(stack * st){
if(st->size)
return false;
else
return true;
}
/**
* @brief Frees the memory allocated for the stack
*
* @param st
*/
void destroyStack(stack* st) {
while (!isEmpty(st)) {
popStack(st);
}
free(st);
}