-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
177 lines (118 loc) · 3.22 KB
/
Copy pathstack.c
File metadata and controls
177 lines (118 loc) · 3.22 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
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
163
164
165
166
167
168
169
170
171
172
173
174
#include "rpncalc.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct rpn_stack_element *rpn_stack_element_create_number(double number)
{
struct rpn_stack_element *ne = malloc(sizeof(struct rpn_stack_element));
if(ne == NULL)
return NULL;
ne->e_type = ET_NUMBER;
ne->value.number = number;
return ne;
}
struct rpn_stack_element *rpn_stack_element_create_string(char *str)
{
if(str == NULL)
return NULL;
struct rpn_stack_element *ne = malloc(sizeof(struct rpn_stack_element));
if(ne == NULL)
return NULL;
ne->e_type = ET_STRING;
/* Make a copy of the string */
ne->value.str = malloc(strlen(str) + 1);
if(ne->value.str == NULL)
return NULL;
strcpy(ne->value.str, str);
return ne;
}
struct rpn_stack_element *rpn_stack_element_create_mark(void)
{
struct rpn_stack_element *ne = malloc(sizeof(struct rpn_stack_element));
if(ne == NULL)
return NULL;
ne->e_type = ET_MARK;
return ne;
}
struct rpn_stack_element *rpn_stack_element_create_bool(char boolean)
{
struct rpn_stack_element *ne = malloc(sizeof(struct rpn_stack_element));
if(ne == NULL)
return NULL;
ne->e_type = ET_BOOL;
ne->value.boolean = boolean;
return ne;
}
struct rpn_stack *rpn_stack_init(size_t max_size)
{
struct rpn_stack *ns = malloc(sizeof(struct rpn_stack));
if(ns == NULL)
return NULL;
ns->contents = malloc(sizeof(struct rpn_stack_element *) * max_size);
if(ns->contents == NULL)
return NULL;
ns->max_size = max_size;
ns->top = -1;
return ns;
}
int rpn_stack_destroy(struct rpn_stack *stack)
{
if(stack == NULL)
return -1;
free(stack->contents);
free(stack);
return 0;
}
int rpn_stack_is_empty(struct rpn_stack *stack)
{
if(stack == NULL)
return -1;
return stack->top < 0;
}
int rpn_stack_is_full(struct rpn_stack *stack)
{
if(stack == NULL)
return -1;
return stack->top >= (stack->max_size - 1);
}
int rpn_stack_push(struct rpn_stack *stack, struct rpn_stack_element *element)
{
if(stack == NULL || element == NULL)
return -1;
if(rpn_stack_is_full(stack))
return 1;
stack->contents[++stack->top] = element;
return 0;
}
struct rpn_stack_element *rpn_stack_pop(struct rpn_stack *stack)
{
if(stack == NULL)
return NULL;
if(rpn_stack_is_empty(stack))
return NULL;
return stack->contents[stack->top--];
}
struct rpn_stack_element *rpn_stack_peek(struct rpn_stack *stack)
{
if(stack == NULL)
return NULL;
if(rpn_stack_is_empty(stack))
return NULL;
return stack->contents[stack->top];
}
int rpn_stack_free(struct rpn_stack *stack)
{
if(stack == NULL)
return -1;
while(!rpn_stack_is_empty(stack))
{
struct rpn_stack_element *e = rpn_stack_pop(stack);
if(e == NULL)
return -1;
if(e->value.str != NULL)
free(e->value.str);
}
free(stack->contents);
free(stack);
return 0;
}