-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
63 lines (56 loc) · 1.17 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
/**
* stack.c
* This file will contain the implementation of the stack.
* In the case of wren the only thing the stack holds is
* integer values, so this can be treated as an array with
* a pointer (index) which moves back and forth along it.
*
* @author Dr. Fenwick
* @version Spring 2014
*/
//Includes
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
void initializeStack(stackType * s)
{
s->top = -1;
}
void stackPush(stackType *s, int item)
{
if(s->top == (STACK_SIZE - 1))
{
printf("Stack push error: stack is full.\nInterpreter terminating.\n");
exit(1);
}
else
{
s->top += 1;
s->items[s->top] = item;
}
}
int stackPop(stackType *s)
{
if (s->top == -1)
{
printf("Stack pop error: stack is empty.\nInterpreter terminating.\n");
exit(1);
}
else
{
int item = s->items[s->top];
s->top -= 1;
return item;
}
}
void printStack(stackType * s)
{
int i;
if (s->top == -1)
{
printf("Stack is empty\n");
return;
}
for (i = s->top; i >= 0; i--)
printf("%d\n", s->items[i]);
}