标签:
matlab堆栈stack杂谈 |
分类: Matlab编程 |
Matlab源代码:堆栈类Stack的实现
-- by benbenknight
)
%-----------------------------------------------
% 文件【test_stack.m】的代码清单:
%-----------------------------------------------
% test_stack.m 测试堆栈对象
a = Stack;
a = init(a,10);
arr = [1 2 3];
for i=1:length(arr)
end
while ~isempty(a)
end
%-----------------------------------------------
% 文件【@Stack\auto_inc.m】的代码清单:
%-----------------------------------------------
function a = auto_inc(a)
% AUTO_INC 自动扩充堆栈容量
%
% a = auto_inc(a)
%
% benbenknight
if a.step_size<=0
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)
%
%
% benbenknight
if sz>0
else
end
%-----------------------------------------------
% 文件【@Stack\isempty.m】的代码清单:
%-----------------------------------------------
function b = isempty(a)
% ISEMPTY 判断堆栈是否为空
%
% b = isempty(a)
%
%
% benbenknight
if a.top<=0
else
end
%-----------------------------------------------
% 文件【@Stack\pop.m】的代码清单:
%-----------------------------------------------
function [a,x] = pop(a)
% POP 将栈顶元素弹出堆栈
%
% [a,x] = pop(a)
%
%
% benbenknight
if isempty(a)
else
end
%-----------------------------------------------
% 文件【@Stack\push.m】的代码清单:
%-----------------------------------------------
function a = push(a,x)
% PUSH 将元素x压入堆栈
%
% a = push(a,x)
%
% benbenknight
% 判断是否需要自动增长堆栈空间
if a.top == a.max_size
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
end
%-----------------------------------------------
% 文件【@Stack\subsasgn.m】的代码清单:
%-----------------------------------------------
function a = subsasgn(a,index,val)
% SUBSASGN 对堆栈数据成员赋值
%
% benbenknight
switch index(1).type
end
%-----------------------------------------------
% 文件【@Stack\subsref.m】的代码清单:
%-----------------------------------------------
function b = subsref(a,index)
% SUBSREF 读取堆栈的数据成员
%
% benbenknight
switch index(1).type
end
%-----------------------------------------------
% 文件【@Stack\top.m】的代码清单:
%-----------------------------------------------
function x = top(a)
% TOP 返回栈顶元素
%
%
% x = top(a)
%
% benbenkight
if isempty(a)
else
end

加载中…