File:  [LON-CAPA] / loncom / types / Stack.pm
Revision 1.3: download - view: text, annotated - select for diffs
Thu Apr 24 10:57:57 2003 UTC (21 years ago) by foxr
Branches: MAIN
CVS tags: HEAD
Added Count member to fetch the number of elements in the data structure.

    1: #   Implement a simple stack in terms of a list.
    2: #
    3: # $Id: Stack.pm,v 1.3 2003/04/24 10:57:57 foxr Exp $
    4: #
    5: # Copyright Michigan State University Board of Trustees
    6: #
    7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    8: #
    9: # LON-CAPA is free software; you can redistribute it and/or modify
   10: # it under the terms of the GNU General Public License as published by
   11: # the Free Software Foundation; either version 2 of the License, or
   12: # (at your option) any later version.
   13: #
   14: # LON-CAPA is distributed in the hope that it will be useful,
   15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17: # GNU General Public License for more details.
   18: #
   19: # You should have received a copy of the GNU General Public License
   20: # along with LON-CAPA; if not, write to the Free Software
   21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   22: #
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: 
   28: =pod
   29: 
   30: =head1 Stack 
   31: 
   32: An object oriented implementation of a Stack data structure.
   33: Stacks are first in last out data structures.
   34: 
   35: =head1 Member functions:
   36: 
   37: =cut
   38: 
   39: package Stack;
   40: 
   41: =pod
   42: 
   43: =head2 new  
   44: 
   45:     Creates a new instance of a stack. 
   46:   
   47:     my $stack = Stack->new();
   48: 
   49: =cut
   50: 
   51: sub new {
   52:     my $class = shift;		# Class name.
   53:     my $self  = [];		# Create an empty list to represent the stack.
   54:     bless($self, $class);	# Turn this into an object and..
   55:     return $self;		# Return it.
   56: }
   57: 
   58: =pod
   59: 
   60: =head2 push
   61: 
   62:     takes an item and pushes it onto the back end of the stack.
   63: 
   64:     my $stack = Stack->new();
   65:     $stack->push(something);
   66: 
   67: =cut
   68: 
   69: sub push {
   70:     my $self = shift;		# Gets the list...
   71:     my $item = shift;		# The item to push.
   72:     push(@$self,$item);
   73: }
   74: 
   75: =pod
   76: 
   77: =head2 pop
   78: 
   79:     Returns the item at the top of the stack: does a pop.
   80: 
   81:     my object = Stack->new();
   82:     my $item = object->pop();
   83: 
   84: =cut
   85: 
   86: sub pop {
   87:     my $self = shift;
   88:     return pop(@$self);
   89: }
   90: 
   91: =pod
   92: =head 1
   93:   Returns the number of items on the stack.
   94: 
   95: =cut
   96: sub Count {
   97:     my $self = shift;
   98:     my $elements = scalar(@$self);
   99:     return $elements;
  100: }
  101: 
  102: 1;
  103: 
  104: =pod
  105: 
  106: =head1 Internal implementation details
  107: 
  108: Stacks are implemented as lists.  Thus a stack is a thinly disguised
  109: list with push and pop wrappers.  Since PERL is a dynamically typed
  110: language, stacks can contain any data type ... including a
  111: heterogenous collection of types.
  112: 
  113: =cut
  114: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>