Skip to content
Snippets Groups Projects
Commit 042e1e5d authored by jacques.grelet_ird.fr's avatar jacques.grelet_ird.fr
Browse files

add stack class (LIFO)

parent 8e6b3756
No related branches found
No related tags found
No related merge requests found
function length = length(self)
length = 0;
node = self.stack_pointer;
while ~isempty(node)
length = length + 1;
node = next(node);
end
end
function element = pop(self)
%increase the stack pointer and return 'top' node data
% usage :
% element = pop(stack)
% $Id$
if isempty(self.stack_pointer)
element = {};
else
element = data(self.stack_pointer);
self.stack_pointer = next(self.stack_pointer);
end
function self = push(self, element)
self.stack_pointer = node(element, self.stack_pointer);
end
function s = stack
%STACK constructor for stack object
% stack is a data structure based on the principle of
% Last In First Out (LIFO)
%
% usage:
% q = stack - Default constructor, empty stack table
%
% example:
% q = stack
% q = push(q, [1:2])
% q = push(q, ones(1,5))
% element = pop(q)
% q = push(q, zeros(1,3))
% etc ...
%
% $Id$
% copy constructor
% ----------------
if nargin == 0
self.stack_pointer = {};
else
error('stack:stack', 'Invalid arguments.');
end
% class constructor
% -----------------
s = class(self,'stack');
function element = top(self)
%return 'top' node
% usage
% data = top(stack)
% $Id: get.m 416 2009-02-13 07:55:41Z jgrelet $
% return object prepared in data
% ------------------------------
if isempty(self.stack_pointer)
element = {};
else
element = data(self.stack_pointer);
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment