File Coverage

blib/lib/DataStructure/LinkedList.pm
Criterion Covered Total %
statement 18 35 51.4
branch n/a
condition 0 3 0.0
subroutine 6 9 66.6
pod 3 3 100.0
total 27 50 54.0


line stmt bran cond sub pod time code
1             package DataStructure::LinkedList;
2              
3 1     1   23906 use 5.006;
  1         4  
  1         89  
4 1     1   8 use strict;
  1         6  
  1         50  
5 1     1   7 use warnings FATAL => 'all';
  1         11  
  1         60  
6 1     1   459 use DataStructure::LinkedList::Cell;
  1         4  
  1         9  
7 1     1   541 use DataStructure::LinkedList::Iterator;
  1         2  
  1         10  
8 1     1   42 use parent qw/Class::Accessor/;
  1         2  
  1         4  
9             __PACKAGE__->mk_accessors(qw/header/);
10              
11             =head1 NAME
12              
13             DataStructure::LinkedList - simple implementation for using LinkedList data structure.
14              
15             =head1 VERSION
16              
17             Version 0.02
18              
19             =cut
20              
21             our $VERSION = '0.02';
22              
23              
24             =head1 SYNOPSIS
25              
26             Quick summary of what the module does.
27              
28             Perhaps a little code snippet.
29              
30             use DataStructure::LinkedList;
31              
32             my $list = new DataStructure::LinkedList;
33             $list->insert(20);
34             $list->insert(15);
35             $list->insert(18);
36             $list->insert(37);
37             $list->insert(3);
38              
39             # display
40             my $iter = $list->iterator;
41             while ($iter->has_next) {
42             print $iter->next->data . "\n";
43             }
44             # you can see result sorted
45             # 3
46             # 15
47             # 18
48             # 20
49             # 37
50             ...
51            
52             # you can also use strings as cells
53             $list = new DataStructure::LinkedList;
54             $list->insert('steeve');
55             $list->insert('hisashi');
56             $list->insert('takairo');
57             $list->insert('kazuyo');
58             $list->insert('jane');
59            
60             # display
61             $iter = $list->iterator;
62             while ($iter->has_next) {
63             print $iter->next->data . "\n";
64             }
65             # you can see result sorted
66             # hisashi
67             # jane
68             # kazuyo
69             # takahiro
70             # steeve
71             ...
72              
73             # you can also use some object as cells
74             $list = new DataStructure::LinkedList;
75             $list->insert(new Person(name => 'lally'));
76             $list->insert(new Person(name => 'hisashi'));
77             $list->insert(new Person(name => 'takairo'));
78             $list->insert(new Person(name => 'kazuyo'));
79             $list->insert(new Person(name => 'jane'));
80            
81             # you have to implements compare_to method in you object.
82             # you have to write sort logic in compare_to method.
83             package Person;
84             use parent qw/Class::Accessor/;
85             __PACKAGE__->mk_accessors(qw/length/);
86             ...
87             sub new {
88             my ($class, %self) = @_;
89             $self{'length'} = length($self{'name'});
90             bless \%self => $class;
91             return \%self;
92             }
93             ...
94             # sort by name's length
95             sub compare_to {
96             my $self = shift;
97             my $cell = shift;
98             return $self->length > $cell->length ? 1 : 0;
99             }
100              
101             =head1 DESCRIPTION
102              
103             This module is simple implementation for using LinkedList data structure.
104             The cells inserted this LinkedList is sorted by value automatically.
105             If you want to sort by original logic, you have to make orignal class and implement compare_to method.
106             You can see SYNOPOSIS as a example;
107              
108             =cut
109              
110             =head1 SUBROUTINES/METHODS
111              
112             =head2 new
113              
114             constructor. Any arguments don't require.
115              
116             =cut
117              
118             sub new {
119 0     0 1   my ($class, %self) = @_;
120 0           $self{'header'} = new DataStructure::LinkedList::Cell("!!List Header!");
121 0           bless \%self => $class;
122 0           return \%self;
123             }
124              
125             =head2 insert($cell)
126              
127             insert a cell on the linked list.
128             You can see SYNOPSIS as a example.
129              
130              
131             =cut
132              
133             sub insert {
134 0     0 1   my $self = shift;
135 0           my $cell = new DataStructure::LinkedList::Cell(shift);
136              
137 0           my $p = $self->header->next;
138 0           my $q = $self->header;
139              
140 0   0       while (defined($p) && $cell->compare_to($p->data) > 0) {
141 0           $q = $p;
142 0           $p = $p->next;
143             }
144              
145 0           my $new_cell = new DataStructure::LinkedList::Cell($cell);
146 0           $new_cell->next($p);
147 0           $q->next($new_cell);
148             }
149              
150             =head2 iterator
151              
152             get a iterator to traverse the linked list.
153             You can see SYNOPSIS as a example.
154              
155             =cut
156              
157             sub iterator {
158 0     0 1   my $self = shift;
159 0           my $iter = DataStructure::LinkedList::Iterator->new($self);
160 0           return $iter;
161             }
162              
163              
164             =head1 AUTHOR
165              
166             Shinchi Takahiro, C<< >>
167              
168             =head1 BUGS
169              
170             Please report any bugs or feature requests to C, or through
171             the web interface at L. I will be notified, and then you'll
172             automatically be notified of progress on your bug as I make changes.
173              
174              
175              
176              
177             =head1 SUPPORT
178              
179             You can find documentation for this module with the perldoc command.
180              
181             perldoc DataStructure::LinkedList
182              
183              
184             You can also look for information at:
185              
186             =over 4
187              
188             =item * RT: CPAN's request tracker (report bugs here)
189              
190             L
191              
192             =item * AnnoCPAN: Annotated CPAN documentation
193              
194             L
195              
196             =item * CPAN Ratings
197              
198             L
199              
200             =item * Search CPAN
201              
202             L
203              
204             =back
205              
206              
207             =head1 ACKNOWLEDGEMENTS
208              
209              
210             =head1 LICENSE AND COPYRIGHT
211              
212             Copyright 2014 Shinchi Takahiro.
213              
214             This program is free software; you can redistribute it and/or modify it
215             under the terms of the the Artistic License (2.0). You may obtain a
216             copy of the full license at:
217              
218             L
219              
220             Any use, modification, and distribution of the Standard or Modified
221             Versions is governed by this Artistic License. By using, modifying or
222             distributing the Package, you accept this license. Do not use, modify,
223             or distribute the Package, if you do not accept this license.
224              
225             If your Modified Version has been derived from a Modified Version made
226             by someone other than you, you are nevertheless required to ensure that
227             your Modified Version complies with the requirements of this license.
228              
229             This license does not grant you the right to use any trademark, service
230             mark, tradename, or logo of the Copyright Holder.
231              
232             This license includes the non-exclusive, worldwide, free-of-charge
233             patent license to make, have made, use, offer to sell, sell, import and
234             otherwise transfer the Package with respect to any patent claims
235             licensable by the Copyright Holder that are necessarily infringed by the
236             Package. If you institute patent litigation (including a cross-claim or
237             counterclaim) against any party alleging that the Package constitutes
238             direct or contributory patent infringement, then this Artistic License
239             to you shall terminate on the date that such litigation is filed.
240              
241             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
242             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
243             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
244             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
245             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
246             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
247             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
248             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
249              
250              
251             =cut
252              
253             1; # End of DataStructure::LinkedList