Avatar of 4d47
0
0
Genius
0
0

4d47's solution

to Linked List in the Perl 6 Track

Instructions
Test suite
Solution

Implement a doubly linked list.

Like an array, a linked list is a simple linear data structure. Several common data types can be implemented using linked lists, like queues, stacks, and associative arrays.

A linked list is a collection of data elements called nodes. In a singly linked list each node holds a value and a link to the next node. In a doubly linked list each node also holds a link to the previous node.

You will write an implementation of a doubly linked list. Implement a Node to hold a value and pointers to the next and previous nodes. Then implement a List which holds references to the first and last node and offers an array-like interface for adding and removing items:

  • push (insert value at back);
  • pop (remove value at back);
  • shift (remove value at front).
  • unshift (insert value at front);

To keep your implementation simple, the tests will not cover error conditions. Specifically: pop or shift will never be called on an empty list.

If you want to know more about linked lists, check Wikipedia.

Resources

Remember to check out the Perl 6 documentation and resources pages for information, tips, and examples if you get stuck.

Running the tests

There is a test suite and module included with the exercise. The test suite (a file with the extension .t) will attempt to run routines from the module (a file with the extension .pm6). Add/modify routines in the module so that the tests will pass! You can view the test data by executing the command perl6 --doc *.t (* being the name of the test suite), and run the test suite for the exercise by executing the command prove . --exec=perl6 in the exercise directory. You can also add the -v flag e.g. prove . --exec=perl6 -v to display all tests, including any optional tests marked as 'TODO'.

Source

Classic computer science topic

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

linked-list.t

#!/usr/bin/env perl6
use v6;
use Test;
use JSON::Fast;
use lib $?FILE.IO.dirname;
use LinkedList;
plan 6;

my Version:D $version = v2;

if LinkedList.^ver !~~ $version {
  warn "\nExercise version mismatch. Further tests may fail!"
    ~ "\nLinkedList is {LinkedList.^ver.gist}. "
    ~ "Test is {$version.gist}.\n";
}

subtest 'Class methods', {
  ok LinkedList.can($_), $_ for <push-list pop-list shift-list unshift-list>;
}

my $cases = from-json $=pod.pop.contents;
for $cases.values -> $case {
  subtest $case.<name>, sub {
    my $linkedlist = LinkedList.new;
    for  |$case.<set> -> %set {
      for %set {
        my $value = $_.value;
        given $_.key  {
          when 'push'    { $linkedlist.?push-list( $value ) }
          when 'unshift' { $linkedlist.?unshift-list( $value ) }
          when 'pop'     { is $linkedlist.?pop-list, $value, 'pop' }
          when 'shift'   { is $linkedlist.?shift-list, $value, 'shift' }
        }
      }
    }
  }
}

=head2 Test Data
=begin code
[
  {
    "set" : [
      { "push" : 10 },
      { "push" : 20 },
      { "pop"  : 20 },
      { "pop"  : 10 }
    ],
    "name" : "push_pop"
  },
  {
    "set" : [
      { "push" : 10  },
      { "push" : 20  },
      { "shift" : 10 },
      { "shift" : 20 }
    ],
    "name" : "push_shift"
  },
  {
    "set" : [
      { "unshift" : 10 },
      { "unshift" : 20 },
      { "shift"   : 20 },
      { "shift"   : 10 }
    ],
    "name" : "unshift_shift"
  },
  {
    "set" : [
      { "unshift" : 10 },
      { "unshift" : 20 },
      { "pop"     : 10 },
      { "pop"     : 20 }
    ],
    "name" : "unshift_pop"
  },
  {
    "set" : [
      { "push"    : 10 },
      { "push"    : 20 },
      { "pop"     : 20 },
      { "push"    : 30 },
      { "shift"   : 10 },
      { "unshift" : 40 },
      { "push"    : 50 },
      { "shift"   : 40 },
      { "pop"     : 50 },
      { "shift"   : 30 }
    ],
    "name" : "example"
  }
]
=end code
class LinkedList is export {

    class Node {
        has Mu $.value is rw;
        has Node $.prev is rw;
        has Node $.next is rw;
    }

    has Node $!first;
    has Node $!last;

    method unshift(Mu $value --> Nil) {
        my $newNode = Node.new(:$value);
        if not $!first {
            $!first = $newNode;
            $!last = $newNode;
            $newNode.prev = Nil;
            $newNode.next = Nil;
        } else {
            self!before($!first, $newNode);
        }
    }

    method push(Mu $value --> Nil) {
        if not $!last {
            self.unshift($value);
        } else {
            self!after($!last, Node.new(:$value));
        }
    }

    method pop(--> Mu) {
        self!remove($!last);
    }

    method shift(--> Mu) {
        self!remove($!first);
    }

    method !after(Node $node, Node $newNode) {
        $newNode.prev = $node;
        if not $node.next {
            $newNode.next = Nil;
            $!last = $newNode;
        } else {
            $newNode.next = $node.next;
            $node.next.prev = $newNode;
        }
        $node.next = $newNode;
    }

    method !before(Node $node, Node $newNode) {
        $newNode.next = $node;
        if not $node.prev {
            $newNode.prev = Nil;
            $!first = $newNode;
        } else {
            $newNode.prev = $node.prev;
            $node.prev.next = $newNode;
        }
        $node.prev = $newNode;
    }

    method !remove(Node $node --> Mu) {
        if not $node.prev {
            $!first = $node.next;
        } else {
            $node.prev.next = $node.next;
        }
        if not $node.next {
            $!last = $node.prev;
        } else {
            $node.next.prev = $node.prev;
        }
        $node.value
    }
}

What can you learn from this solution?

A huge amount can be learnt from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that I could read more about to develop my understanding?