-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
76 lines (66 loc) · 1.16 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
/*
* These routines manage a stack of scalars or matrices for a simple stack
* oriented matrix/scalar calculater
*/
#include <stdio.h>
#include "mem.h"
#include "types.h"
#include "constant.h"
#include "functions.h"
static struct item stack[STACKSIZE];
static int sp = 0;
void push (item)
struct item item;
{
if (sp == STACKSIZE) {
printf ("Stack limit exceeded\n");
exit (1);
}
else if ((item.datatype == SCALAR) ||
(item.datatype == MATRIX) ||
(item.datatype == NAME)) {
stack[sp++] = item;
}
else { /* Can't happen */
printf ("Internal inconsistancy in push\n");
exit (1);
}
}
struct item pop ()
{
struct item value;
if (sp == 0) {
printf ("Stack is empty!\n");
value.datatype = EMPTY;
}
else {
value = stack[--sp];
}
return (value);
}
void print_stack()
{
int i;
for (i=0; i<sp-1; i++) {
printf ("%d: ", sp-i);
print_item (stack[i]);
printf ("\n");
}
}
void print_top()
{
if (sp != 0) {
printf ("%d: ", 1);
print_item (stack[sp-1]);
printf ("\n");
}
}
static int mark;
void mark_stack()
{
mark = sp;
}
void reset_stack()
{
sp = mark;
}