-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.m
238 lines (216 loc) · 7.14 KB
/
Stack.m
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
classdef Stack < handle
%--------------------------------------------------------------------------
% Class: Stack < handle
%
% Constructor: S = Stack(n);
% S = Stack(n,x0);
%
% Properties: (none)
%
% Methods: S.Push(xi);
% xi = S.Pop();
% count = S.Count();
% capacity = S.Capacity();
% bool = S.IsEmpty();
% bool = S.IsFull();
% S.Clear();
%
% Description: This class implements a first-in last-out (FILO) stack of
% numeric values
%
% Author: Brian Moore
%
% Date: January 16, 2014
%--------------------------------------------------------------------------
%
% Private properties
%
properties (Access = private)
k; % current number of elements
n; % stack capacity
x; % stack array
end
%
% Public methods
%
methods (Access = public)
%
% Constructor
%
function this = Stack(n,x0)
%----------------------- Constructor --------------------------
% Syntax: S = Stack(n);
% S = Stack(n,x0);
%
% Inputs: n is the maximum number of values that S can
% hold
%
% x0 is a vector (of length <= n) of numbers to
% add to the stack during initialization
%
% Description: Creates a FILO stack with capacity n
%--------------------------------------------------------------
% Initialize stack
if (n == 0)
Stack.ZeroCapacityError();
end
this.n = n;
this.x = nan(n,1);
if ((nargin == 2) && ~isempty(x0))
% Insert given elements
k0 = numel(x0);
if (k0 > n)
% Stack overflow
Stack.OverflowError();
else
this.x(1:k0) = x0(:);
this.SetLength(k0);
end
else
% Empty stack
this.Clear();
end
end
%
% Push element onto stack
%
function Push(this,xi)
%--------------------------- Push -----------------------------
% Syntax: S.Push(xi);
%
% Inputs: xi is a numeric value
%
% Description: Adds value xi to S
%--------------------------------------------------------------
this.SetLength(this.k + 1);
this.x(this.k) = xi;
end
%
% Pop element from stack
%
function xi = Pop(this)
%--------------------------- Pop ------------------------------
% Syntax: xi = S.Pop();
%
% Inputs: xi is a numeric value
%
% Description: Removes top value from S
%--------------------------------------------------------------
this.SetLength(this.k - 1);
xi = this.x(this.k + 1);
end
%
% Return number of elements in stack
%
function count = Count(this)
%-------------------------- Count -----------------------------
% Syntax: count = S.Count();
%
% Outputs: count is the number of values in S
%
% Description: Returns the number of values in S
%--------------------------------------------------------------
count = this.k;
end
%
% Return stack capacity
%
function capacity = Capacity(this)
%------------------------- Capacity ---------------------------
% Syntax: capacity = S.Capacity();
%
% Outputs: capacity is the size of S
%
% Description: Returns the maximum number of values that can
% fit in S
%--------------------------------------------------------------
capacity = this.n;
end
%
% Check for empty stack
%
function bool = IsEmpty(this)
%------------------------- IsEmpty ----------------------------
% Syntax: bool = S.IsEmpty();
%
% Outputs: bool = {true,false}
%
% Description: Determines if S is empty (i.e., contains zero
% values)
%--------------------------------------------------------------
if (this.k == 0)
bool = true;
else
bool = false;
end
end
%
% Check for full stack
%
function bool = IsFull(this)
%-------------------------- IsFull ----------------------------
% Syntax: bool = S.IsFull();
%
% Outputs: bool = {true,false}
%
% Description: Determines if S is full
%--------------------------------------------------------------
if (this.k == this.n)
bool = true;
else
bool = false;
end
end
%
% Clear the stack
%
function Clear(this)
%-------------------------- Clear -----------------------------
% Syntax: S.Clear();
%
% Description: Removes all values from S
%--------------------------------------------------------------
this.SetLength(0);
end
end
%
% Private methods
%
methods (Access = private)
%
% Set length
%
function SetLength(this,k)
if (k < 0)
Stack.UnderflowError();
elseif (k > this.n)
Stack.OverflowError();
end
this.k = k;
end
end
%
% Private static methods
%
methods (Access = private, Static = true)
%
% Overflow error
%
function OverflowError()
error('Stack overflow');
end
%
% Underflow error
%
function UnderflowError()
error('Stack underflow');
end
%
% No capacity error
%
function ZeroCapacityError()
error('Stack with no capacity is not allowed');
end
end
end