Annotation of loncom/types/Stack.pm, revision 1.4
1.2 albertel 1: # Implement a simple stack in terms of a list.
2: #
1.4 ! albertel 3: # $Id: Stack.pm,v 1.3 2003/04/24 10:57:57 foxr Exp $
1.2 albertel 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/
1.1 foxr 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:
1.3 foxr 91: =pod
1.4 ! albertel 92:
1.3 foxr 93: =head 1
1.4 ! albertel 94:
1.3 foxr 95: Returns the number of items on the stack.
96:
97: =cut
1.4 ! albertel 98:
1.3 foxr 99: sub Count {
100: my $self = shift;
101: my $elements = scalar(@$self);
102: return $elements;
103: }
1.1 foxr 104:
105: 1;
106:
107: =pod
108:
109: =head1 Internal implementation details
110:
111: Stacks are implemented as lists. Thus a stack is a thinly disguised
112: list with push and pop wrappers. Since PERL is a dynamically typed
113: language, stacks can contain any data type ... including a
114: heterogenous collection of types.
115:
116: =cut
117:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>