-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_sim.c
131 lines (115 loc) · 3.2 KB
/
stack_sim.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
/***********************************************************************************
Copyright 2014 Arizona Board of Regents; all rights reserved.
This software is being provided by the copyright holders under the
following license. By obtaining, using and/or copying this software, you
agree that you have read, understood, and will comply with the following
terms and conditions:
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee or royalty is hereby granted,
provided that the full text of this notice appears on all copies of the
software and documentation or portions thereof, including modifications,
that you make.
This software is provided "as is," and copyright holders make no
representations or warranties, expressed or implied. By way of example, but
not limitation, copyright holders make no representations or warranties of
merchantability or fitness for any particular purpose or that the use of the
software or documentation will not infringe any third party patents,
copyrights, trademarks or other rights. Copyright holders will bear no
liability for any use of this software or documentation.
The name and trademarks of copyright holders may not be used in advertising
or publicity pertaining to the software without specific, written prior
permission. Title to copyright in this software and any associated
documentation will at all times remain with copyright holders.
***********************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "stack_sim.h"
/*
* Illustration of the stack
* sp alway points to the first empty slot in the
* (underlyding) list, so if sp==0, stack is empty
*
* | a |
* -----
* | b |
* ----- <-sp
*
*/
OpStack *initOpStack(void (* printFunc)(void *)){
OpStack *s = (OpStack *)malloc(sizeof(*s));
if(!s)
return NULL;
s->size = 32;
s->sp = 0;
s->stack = (void *)malloc(s->size * sizeof(void *));
if(!(s->stack)){
free(s);
return NULL;
}
s->printFunc = printFunc?printFunc:NULL;
return s;
}
void destroyOpStack(OpStack *s){
if(!s)
return;
free(s->stack);
free(s);
s=NULL;
return;
}
void pushOpStack(OpStack *s, void *item){
assert(s);
void *temp;
if(s->sp >= s->size){
//fprintf(stderr, "resize OpStack\n");
temp = realloc((void *)(s->stack), 2*(s->size)*sizeof(void *));
if(temp==NULL){
fprintf(stderr, "ERROR: Memory shortage while resize OpStack\n");
assert(0);
}
s->size = s->size*2;
s->stack = temp;
}
s->stack[s->sp++] = item;
return;
}
void *popOpStack(OpStack *s){
assert(s);
if(s->sp==0){
fprintf(stderr, "ERROR: Pop empty stack\n");
return NULL;
}
return s->stack[--s->sp];
}
void *peekOpStack(OpStack *s){
assert(s);
if(s->sp==0)
return NULL;
else
return s->stack[s->sp-1];
}
bool isOpStackEmpty(OpStack *s){
assert(s);
if(s->sp==0)
return true;
else
return false;
}
void printOpStack(OpStack *s){
assert(s);
int i;
for(i=0;i<s->sp;i++){
if(s->printFunc==NULL)
printf("#%-4d |_%16p_|\n", i, s->stack[i]);
else{
printf("#%-4d |", i);
s->printFunc(s->stack[i]);
printf("\n");
}
}
}
int countOpStack(OpStack *s){
assert(s);
return s->sp;
}