exercise: LinkedList version: 2 plan: 6 modules: - use: JSON::Fast methods: 'push-list pop-list shift-list unshift-list' tests: |- my $cases = from-json $=pod.pop.contents; for $cases.values -> $case { subtest $case., sub { my $linkedlist = LinkedList.new; for |$case. -> %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 unit: class example: |- class ListNode { has $.next is rw; has $.previous is rw; has $.value; } has $!first; has $!last; method push-list($value) { my $next = ListNode.new( value => $value, previous => $!last ); if ( $!last ) { $!last = $!last.next= $next ; } else { $!first = $!last = $next; } } method unshift-list($value) { my $next = ListNode.new( value => $value, next => $!first ); if ( $!first ) { $!first = $!first.previous = $next ; } else { $!first = $!last = $next; } } method shift-list() { my $f = $!first; $!first = $!first.next; unless $!first { $!last = $!first } return $f.value; } method pop-list() { my $f = $!last; $!last = $!last.previous; unless $!.last { $!first = $!last } return $f.value; }