Daniel@0: classdef CStack < handle Daniel@0: % CStack define a stack data strcuture Daniel@0: % Daniel@0: % It likes java.util.Stack, however, it could use CStack.content() to Daniel@0: % return all the data (in cells) of the Stack, and it is a litter faster Daniel@0: % than java's Stack. Daniel@0: % Daniel@0: % s = CStack(c); c is a cells, and could be omitted Daniel@0: % s.size() return the numble of element Daniel@0: % s.isempty() return true when the stack is empty Daniel@0: % s.empty() delete the content of the stack Daniel@0: % s.push(el) push el to the top of stack Daniel@0: % s.pop() pop out the top of the stack, and return the element Daniel@0: % s.top() return the top element of the stack Daniel@0: % s.remove() remove all the elements in the stack Daniel@0: % s.content() return all the data of the stack (in the form of a Daniel@0: % cells with size [s.size(), 1] Daniel@0: % Daniel@0: % See also CList, CQueue Daniel@0: % Daniel@0: % Copyright: zhang@zhiqiang.org, 2010. Daniel@0: % url: http://zhiqiang.org/blog/it/matlab-data-structures.html Daniel@0: Daniel@0: properties (Access = private) Daniel@0: buffer % 一个cell数组,保存栈的数据 Daniel@0: cur % 当前元素位置, or the length of the stack Daniel@0: capacity % 栈的容量,当容量不够时,容量扩充为2倍。 Daniel@0: end Daniel@0: Daniel@0: methods Daniel@0: function obj = CStack(c) Daniel@0: if nargin >= 1 && iscell(c) Daniel@0: obj.buffer = c(:); Daniel@0: obj.cur = numel(c); Daniel@0: obj.capacity = obj.cur; Daniel@0: elseif nargin >= 1 Daniel@0: obj.buffer = cell(100, 1); Daniel@0: obj.cur = 1; Daniel@0: obj.capacity =100; Daniel@0: obj.buffer{1} = c; Daniel@0: else Daniel@0: obj.buffer = cell(100, 1); Daniel@0: obj.capacity = 100; Daniel@0: obj.cur = 0; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function s = size(obj) Daniel@0: s = obj.cur; Daniel@0: end Daniel@0: Daniel@0: function remove(obj) Daniel@0: obj.cur = 0; Daniel@0: end Daniel@0: Daniel@0: function b = empty(obj) Daniel@0: b = obj.cur; Daniel@0: obj.cur = 0; Daniel@0: end Daniel@0: Daniel@0: function b = isempty(obj) Daniel@0: b = ~logical(obj.cur); Daniel@0: end Daniel@0: Daniel@0: function push(obj, el) Daniel@0: if obj.cur >= obj.capacity Daniel@0: obj.buffer(obj.capacity+1:2*obj.capacity) = cell(obj.capacity, 1); Daniel@0: obj.capacity = 2*obj.capacity; Daniel@0: end Daniel@0: obj.cur = obj.cur + 1; Daniel@0: obj.buffer{obj.cur} = el; Daniel@0: end Daniel@0: Daniel@0: function el = top(obj) Daniel@0: if obj.cur == 0 Daniel@0: el = []; Daniel@0: warning('CStack:No_Data', 'trying to get top element of an emtpy stack'); Daniel@0: else Daniel@0: el = obj.buffer{obj.cur}; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function el = pop(obj) Daniel@0: if obj.cur == 0 Daniel@0: el = []; Daniel@0: warning('CStack:No_Data', 'trying to pop element of an emtpy stack'); Daniel@0: else Daniel@0: el = obj.buffer{obj.cur}; Daniel@0: obj.cur = obj.cur - 1; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function display(obj) Daniel@0: if obj.cur Daniel@0: for i = 1:obj.cur Daniel@0: disp([num2str(i) '-th element of the stack:']); Daniel@0: disp(obj.buffer{i}); Daniel@0: end Daniel@0: else Daniel@0: disp('The stack is empty'); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: function c = content(obj) Daniel@0: c = obj.buffer(1:obj.cur); Daniel@0: end Daniel@0: end Daniel@0: end