Matlab源代码:堆栈类Stack的实现
-- by benbenknight
自己编写了一个堆栈类Stack,供大家分享。
类的成员函数清单如下:
Stack STACK 堆栈对象Stack的构造函数 auto_inc AUTO_INC 自动扩充堆栈容量 display DISPLAY 显示堆栈内容 empty EMPTY 清空堆栈 init INIT 初始化堆栈 isempty ISEMPTY 判断堆栈是否为空 pop POP 将栈顶元素弹出堆栈 push PUSH 将元素x压入堆栈 subsasgn SUBSASGN 对堆栈数据成员赋值 subsref SUBSREF 读取堆栈的数据成员 top TOP 返回栈顶元素
源代码清单如下:
(由于无法添加附件,只能出此下策,请大家原谅啊
)
%-----------------------------------------------
% 文件【test_stack.m】的代码清单:
%-----------------------------------------------
% test_stack.m 测试堆栈对象
a = Stack;
a = init(a,10);
arr = [1 2 3];
for i=1:length(arr)
a = push(a,arr(i));
a.top
a.stack
end
while ~isempty(a)
[a,x] = pop(a);
x
a.top
a.stack
end
%-----------------------------------------------
% 文件【@Stack\auto_inc.m】的代码清单:
%-----------------------------------------------
function a = auto_inc(a)
% AUTO_INC 自动扩充堆栈容量
%
% a = auto_inc(a)
%
% benbenknight
if a.step_size<=0
a.step_size = 100;
end
a.max_size = a.max_size + a.step_size;
a.stack = [a.stack;zeros(a.step_size,1)];
%-----------------------------------------------
% 文件【@Stack\display.m】的代码清单:
%-----------------------------------------------
function display(p)
% DISPLAY 显示堆栈内容
%
% display(p)
%
% benbenknight
disp(struct(p));
%-----------------------------------------------
% 文件【@Stack\empty.m】的代码清单:
%-----------------------------------------------
function a = empty(a)
% EMPTY 清空堆栈
%
% a = empty(a)
%
% benbenknight
a.top = 0;
%-----------------------------------------------
% 文件【@Stack\init.m】的代码清单:
%-----------------------------------------------
function a = init(a,sz)
% INIT 初始化堆栈
%
% a = init(a,sz)
% sz:堆栈的初始容量大小。
%
% benbenknight
if sz>0
a.max_size = sz;
a.stack = zeros(sz,1);
else
error('Invalid stack size');
end
%-----------------------------------------------
% 文件【@Stack\isempty.m】的代码清单:
%-----------------------------------------------
function b = isempty(a)
% ISEMPTY 判断堆栈是否为空
%
% b = isempty(a)
% b:如果堆栈为空,返回1;否则,返回0。
%
% benbenknight
if a.top<=0
b = 1;
else
b = 0;
end
%-----------------------------------------------
% 文件【@Stack\pop.m】的代码清单:
%-----------------------------------------------
function [a,x] = pop(a)
% POP 将栈顶元素弹出堆栈
%
% [a,x] = pop(a)
% x:输出参数,保存弹出的栈顶元素
%
% benbenknight
if isempty(a)
x = [];
else
x = a.stack(a.top); a.stack(a.top) = 0;
a.top = a.top-1;
end
%-----------------------------------------------
% 文件【@Stack\push.m】的代码清单:
%-----------------------------------------------
function a = push(a,x)
% PUSH 将元素x压入堆栈
%
% a = push(a,x)
%
% benbenknight % 判断是否需要自动增长堆栈空间
if a.top == a.max_size
a = auto_inc(a);
end
% 添加
a.top = a.top + 1;
a.stack(a.top) = x;
%-----------------------------------------------
% 文件【@Stack\Stack.m】的代码清单:
%-----------------------------------------------
function a = Stack(varargin)
% STACK 堆栈对象Stack的构造函数
%
% a = Stack(varargin)
%
% benbenknight
switch nargin
case 0
a.top = 0; % 栈顶元素的下标
a.max_size = 0; % 堆栈的最大容量
a.step_size = 100; %
堆栈自动扩大容量时的步长
a.stack = []; % 堆栈元素数组
a = class(a,'Stack');
case 1
if isa(varargin{1},'Stack')
a =
varargin{1};
else
error('Wrong
argument type')
end
otherwise
error('Wrong Number of input arguments')
end
%-----------------------------------------------
% 文件【@Stack\subsasgn.m】的代码清单:
%-----------------------------------------------
function a = subsasgn(a,index,val)
% SUBSASGN 对堆栈数据成员赋值
%
% benbenknight
switch index(1).type
case '.'
switch
index(1).subs
case 'top'
a.top = val;
case 'max_size'
a.max_size = val;
case 'step_size'
a.step_size = val;
case
'stack' if length(index)==2
a.stack(index(2).subs{:}) = val;
else
a.stack = val;
end end
end
%-----------------------------------------------
% 文件【@Stack\subsref.m】的代码清单:
%-----------------------------------------------
function b = subsref(a,index)
% SUBSREF 读取堆栈的数据成员
%
% benbenknight
switch index(1).type
case '.'
switch
index(1).subs
case 'top'
b = a.top;
case 'max_size'
b = a.max_size;
case 'step_size'
b = a.step_size;
case 'stack'
if length(index)==2
b = a.stack(index(2).subs{:});
else
b = a.stack;
end end
end
%-----------------------------------------------
% 文件【@Stack\top.m】的代码清单:
%-----------------------------------------------
function x = top(a)
% TOP 返回栈顶元素
% 注意,并不弹出堆栈
%
% x = top(a)
%
% benbenkight
if isempty(a)
x = [];
else x = a.stack(a.top);
end