-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodestack.c
162 lines (125 loc) · 3.1 KB
/
nodestack.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "nodestack.h"
NODESTACK* initStack()
{
NODESTACK* nodestack = calloc(1,sizeof(NODESTACK));
if (nodestack == NULL)
{
error("Error allocating new stack struct");
exit(MALLOC_ERR_EXIT_CODE);
}
nodestack->nodes = calloc(START_NODESTACK_SIZE,sizeof(NODE));
if (nodestack->nodes == NULL)
{
error("Error allocating stack nodes array");
exit(MALLOC_ERR_EXIT_CODE);
}
nodestack->size = START_NODESTACK_SIZE;
nodestack->topIndex = -1;
nodestack->bottomIndex = -1;
return nodestack;
}
int stackIsEmpty(NODESTACK* nodestack)
{
return (nodestack->topIndex == -1);
}
int stackIsEmptyAsQueue(NODESTACK* nodestack)
{
return (nodestack->topIndex+1 == nodestack->bottomIndex);
}
int stackIsFull(NODESTACK* nodestack)
{
return (nodestack->topIndex + 1 == nodestack->size);
}
NODE popNode(NODESTACK* nodestack)
{
if (stackIsEmpty(nodestack))
{
error("Attempt to pop from empty stack");
exit(GENERAL_ERR_CODE);
}
nodestack->topIndex--;
return nodestack->nodes[nodestack->topIndex + 1];
}
NODE popNodeFromBottom(NODESTACK* nodestack)
{
if (nodestack->bottomIndex == (nodestack->topIndex + 1))
{
error("Attempt to pop from bottom of empty stack");
exit(GENERAL_ERR_CODE);
}
nodestack->bottomIndex++;
return nodestack->nodes[nodestack->bottomIndex - 1];
}
NODE topNodeValue(NODESTACK* nodestack)
{
if (stackIsEmpty(nodestack))
{
error("Attempt to access a value of top node of empty stack");
exit(GENERAL_ERR_CODE);
}
return nodestack->nodes[nodestack->topIndex];
}
NODE* topNode(NODESTACK* nodestack)
{
if (stackIsEmpty(nodestack))
{
error("Attempt to access top node of empty stack");
exit(GENERAL_ERR_CODE);
}
return &(nodestack->nodes[nodestack->topIndex]);
}
void pushNode(NODESTACK* nodestack, NODE node)
{
if (stackIsFull(nodestack))
{
nodestack->size += 200;
nodestack->nodes = realloc(nodestack->nodes, nodestack->size);
if (nodestack->nodes == NULL)
{
error("Cannot reallocate node stack: out of memory");
exit(GENERAL_ERR_CODE);
}
}
if (nodestack->bottomIndex == -1)
{
nodestack->bottomIndex++;
}
(nodestack->topIndex) += 1;
nodestack->nodes[nodestack->topIndex] = node;
}
void pushValue(NODESTACK* nodestack, complexDouble value)
{
if (stackIsFull(nodestack))
{
nodestack->size += 200;
nodestack->nodes = realloc(nodestack->nodes, nodestack->size);
if (nodestack->nodes == NULL)
{
error("Cannot reallocate node stack in pushValue: out of memory");
exit(GENERAL_ERR_CODE);
}
}
nodestack->topIndex++;
NODE* newNode = initValueNodeFromValue(value._Val[0],value._Val[1]);
nodestack->nodes[nodestack->topIndex] = *newNode;
free(newNode);
return;
}
void pushOperator(NODESTACK* nodestack, char operatorChar)
{
if (stackIsFull(nodestack))
{
nodestack->size += 200;
nodestack->nodes = realloc(nodestack->nodes, nodestack->size);
if (nodestack->nodes == NULL)
{
error("Cannot reallocate node stack in pushOperator: out of memory");
exit(GENERAL_ERR_CODE);
}
}
nodestack->topIndex++;
NODE* newNode = initOperatorNodeFromValue(operatorChar);
nodestack->nodes[nodestack->topIndex] = *newNode;
free(newNode);
return;
}