-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCStack.m
More file actions
executable file
·126 lines (114 loc) · 3.56 KB
/
CStack.m
File metadata and controls
executable file
·126 lines (114 loc) · 3.56 KB
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
classdef CStack < handle
% CStack define a stack data strcuture
%
% It likes java.util.Stack, however, it could use CStack.content() to
% return all the data (in cells) of the Stack, and it is a litter faster
% than java's Stack.
%
% s = CStack(c); c is a cells, and could be omitted
% s.size() return the numble of element
% s.isempty() return true when the stack is empty
% s.empty() delete the content of the stack
% s.push(el) push el to the top of stack
% s.pop() pop out the top of the stack, and return the element
% s.top() return the top element of the stack
% s.remove() remove all the elements in the stack
% s.content() return all the data of the stack (in the form of a
% cells with size [s.size(), 1]
%
% See also CList, CQueue
%
% ??
% s = CStack; ???
% s = CStack(c); ??c?sc?cell?c????
% c????
%
% ??
% sz = s.size() ?????????
% s.isempty() ????
% s.empty() ?
% s.push(el) ?el??
% s.pop() ???û????
% el = s.top() ???û????
% s.remove() ?
% s.content() ??s???cell
%
% See also CList, CQueue
%
% Copyright: zhang@zhiqiang.org, 2010.
% url: http://zhiqiang.org/blog/it/matlab-data-structures.html
properties (Access = private)
buffer % ?cell??
cur % ???, or the length of the stack
capacity % ???2
end
methods
function obj = CStack(c)
if nargin >= 1 && iscell(c)
obj.buffer = c(:);
obj.cur = numel(c);
obj.capacity = obj.cur;
elseif nargin >= 1
obj.buffer = cell(100, 1);
obj.cur = 1;
obj.capacity =100;
obj.buffer{1} = c;
else
obj.buffer = cell(100, 1);
obj.capacity = 100;
obj.cur = 0;
end
end
function s = size(obj)
s = obj.cur;
end
function remove(obj)
obj.cur = 0;
end
function b = empty(obj)
b = obj.cur;
obj.cur = 0;
end
function b = isempty(obj)
b = ~logical(obj.cur);
end
function push(obj, el)
if obj.cur >= obj.capacity
obj.buffer(obj.capacity+1:2*obj.capacity) = cell(obj.capacity, 1);
obj.capacity = 2*obj.capacity;
end
obj.cur = obj.cur + 1;
obj.buffer{obj.cur} = el;
end
function el = top(obj)
if obj.cur == 0
el = [];
warning('CStack:No_Data', 'trying to get top element of an emtpy stack');
else
el = obj.buffer{obj.cur};
end
end
function el = pop(obj)
if obj.cur == 0
el = [];
warning('CStack:No_Data', 'trying to pop element of an emtpy stack');
else
el = obj.buffer{obj.cur};
obj.cur = obj.cur - 1;
end
end
function display(obj)
if obj.cur
for i = 1:obj.cur
disp([num2str(i) '-th element of the stack:']);
disp(obj.buffer{i});
end
else
disp('The stack is empty');
end
end
function c = content(obj)
c = obj.buffer(1:obj.cur);
end
end
end