Browse Source

fixing loop structure and adding more comments to Perl linked list implementations.

master
Charles Reid 5 years ago
parent
commit
3443ecf683
  1. 38
      perl/LinkedList1.pl
  2. 26
      perl/LinkedList2.pl
  3. 11
      perl/LinkedList3.pl

38
perl/LinkedList1.pl

@ -14,37 +14,47 @@ print "Linked Lists using Reference Variables/Pointers:\n";
# Reference Variables, or Pointers
#
# These "links" are just integer index values, indicating which node points to which other node.
@links = qw( 1 2 3 4 5 6 Null );
print "Using Pointers\n";
# These "nodes" are all initialized with data (colors) and links (Null)
@nodes = qw (blue:Null green:Null yellow:Null orange:Null red:Null purple:Null brown:Null);
for ($i = 0; $i <= $#nodes; $i++)
{ $ptr = \$nodes[$i];
# Now use a for loop to update each node's link, using @links
for ($i = 0; $i <= $#nodes; $i++) {
# Get a pointer to this node
$ptr = \$nodes[$i];
# The data "blue:Null" is a string that we need to split.
# data[0] will contain the data
# data[1] will contain the link
@data = split(/:/,$$ptr);
print "Before: $ptr @data ";
# Update the link using @links at i
$data[1] = $links[$i];
print "-> @data \n";
# Now that we've updated the link, we need to re-combine the data and link, and put it back in the array.
$$ptr = join ':',@data;
}
}
# Print out the new linked list nodes array:
print "\n\n";
print "@nodes";
print "\n\n";
print "Traversing list:\n";
# Print the linked list:
print "Traversing list:\n";
$next = 0;
while ($next !~ "Null")
{@data = split(":",$nodes[$next]);
print $data[0], "\n";
$next = $data[1];
}
while ($next !~ "Null") {
@data = split(":",$nodes[$next]);
print $data[0], "\n";
$next = $data[1];
}
print "\n\n"

26
perl/LinkedList2.pl

@ -10,21 +10,31 @@
print "Linked Lists using a Hash:\n";
# Initializing a hash using the "correspond" operator to make easy reading
# Initializing a hash using the "correspond" operator => to make easy reading
# keys are unix commands and contain the data.
# values are desriptions, plus a link to the next node.
%hash = (
"man" => "Get UNIX Help:more",
"cat" => "Display Files:Null",
"more"=> "Page Through Files:cat");
print "Traversing list:\n";
# Head node:
$next = "man";
while ($next !~ "Null")
{ @data = split(/:/, $hash{$next});
print "$next $data[0] \n";
$next = $data[1];
}
print "\n\n";
# Note that Perl doesn't allow us to do much directly with memory,
# so this is a bit of a hacky implementation.
while ($next !~ "Null") {
# Split (unix description):(link) into the array @data
@data = split(/:/, $hash{$next});
# Grab the link to the next node
print "$next $data[0] \n";
# Get the next node
$next = $data[1];
}
print "\n\n";

11
perl/LinkedList3.pl

@ -8,15 +8,14 @@
# March 2013
# Adding comments
print "Linked Lists using 2D Array:\n";
#
# This is not a "true"/useful linked list implementation,
# since it usees an array, not objects.
# There's no need to use a linked list in this case.
# There's no savings from using a linked list in Perl...
# Method #1 using 2D Arrays
sub print_list {
$max = $_[0];
@ -64,9 +63,9 @@ $next = 0;
#Step through Linked List
print "Traversing list:\n";
while ($next !~ "Null"){
print "$list[$next][0] \n";
$next = $list[$next][1];
while ($next !~ "Null") {
print "$list[$next][0] \n";
$next = $list[$next][1];
}
print "\n\n";

Loading…
Cancel
Save