1: #
2: # Implement a simple queue in terms of a list.
3: #
4:
5: =pod
6:
7: =head1 Queue
8:
9: An object oriented implementation of a queue data structure. queues
10: are first in last out data structures.
11:
12: =head1 Member functions:
13:
14: =cut
15:
16: package Queue;
17:
18:
19: =pod
20:
21: =head2 new
22:
23: Construct a new queue.
24:
25: =cut
26:
27: sub new {
28: my $class = shift; # Class name to bless into.
29: my $self = []; # Our data is a reference to an anon. array.
30:
31: bless($self, $class); # Type this as an object.
32:
33: return $self;
34: }
35:
36: =pod
37:
38: =head2 enqueue
39:
40: Add an element to the tail of the queue.
41:
42: =cut
43:
44: sub enqueue {
45: my $self = shift; # Get object reference.
46: my $item = shift; # Get the item to enqueue...
47: push(@$self, $item); # Push the item to the back of the array.
48:
49:
50: }
51:
52: =pod
53:
54: =head2 dequeue
55:
56: Remove an element from the front of the queue.
57:
58: =cut
59:
60: sub dequeue {
61: my $self = shift; # Get object reference....
62: return shift @$self; # Remove from the front of the queue.
63: }
64:
65:
66: 1;
67:
68: =pod
69:
70: =head1 Theory
71:
72: The queue is implemented as an array... enqueue is a thinly disguised
73: push, and dequeue is a thinly disguised shift. This is probably quite
74: in efficient for large queues, but should be fine for reasonably sized
75: queues.
76:
77: Note that since Perl is a dynamically typed language, queues can
78: contain objects of any data type and can even be heterogenously typed.
79:
80: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>