From 042e1e5dd20fad466d8de7101091ee382a80d0cd Mon Sep 17 00:00:00 2001
From: Jacques Grelet <jacques.grelet@ird.fr>
Date: Tue, 17 Feb 2009 07:50:16 +0000
Subject: [PATCH] add stack class (LIFO)

---
 @stack/length.m |  9 +++++++++
 @stack/pop.m    | 14 ++++++++++++++
 @stack/push.m   |  4 ++++
 @stack/stack.m  | 29 +++++++++++++++++++++++++++++
 @stack/top.m    | 17 +++++++++++++++++
 5 files changed, 73 insertions(+)
 create mode 100644 @stack/length.m
 create mode 100644 @stack/pop.m
 create mode 100644 @stack/push.m
 create mode 100644 @stack/stack.m
 create mode 100644 @stack/top.m

diff --git a/@stack/length.m b/@stack/length.m
new file mode 100644
index 0000000..0aa286e
--- /dev/null
+++ b/@stack/length.m
@@ -0,0 +1,9 @@
+   function length = length(self)
+      length = 0;
+      node = self.stack_pointer;
+      while ~isempty(node)
+        length = length + 1;
+        node = next(node);
+      end
+    end
+
diff --git a/@stack/pop.m b/@stack/pop.m
new file mode 100644
index 0000000..9b647c1
--- /dev/null
+++ b/@stack/pop.m
@@ -0,0 +1,14 @@
+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
+
diff --git a/@stack/push.m b/@stack/push.m
new file mode 100644
index 0000000..7177032
--- /dev/null
+++ b/@stack/push.m
@@ -0,0 +1,4 @@
+  function self = push(self, element)
+      self.stack_pointer = node(element, self.stack_pointer);
+    end
+
diff --git a/@stack/stack.m b/@stack/stack.m
new file mode 100644
index 0000000..0c46a33
--- /dev/null
+++ b/@stack/stack.m
@@ -0,0 +1,29 @@
+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');
diff --git a/@stack/top.m b/@stack/top.m
new file mode 100644
index 0000000..3927b5b
--- /dev/null
+++ b/@stack/top.m
@@ -0,0 +1,17 @@
+  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
+
-- 
GitLab