-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathStack.m
45 lines (39 loc) · 1.11 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
classdef Stack < handle
%INTEGERSTACK Summary of this class goes here
% Detailed explanation goes here
properties
data
height
currentIndex
end
methods
function obj = Stack(size, width)
w = 1;
if nargin > 1
w = width;
end
obj.data = zeros(size, w);
obj.height = size;
obj.currentIndex = 0;
end
function push(obj, v)
if (obj.currentIndex < obj.height)
obj.currentIndex = obj.currentIndex + 1;
obj.data(obj.currentIndex, :) = v;
else
error('Stack is full');
end
end
function v = pop(obj)
if (obj.currentIndex > 0)
v = obj.data(obj.currentIndex, :);
obj.currentIndex = obj.currentIndex - 1;
else
error('Stack is empty');
end
end
function c = count(obj)
c = obj.currentIndex;
end
end
end