How to resolve the algorithm Doubly-linked list/Definition step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Doubly-linked list/Definition step by step in the Raku programming language

Table of Contents

Problem Statement

Define the data structure for a complete Doubly Linked List.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Doubly-linked list/Definition step by step in the Raku programming language

Source code in the raku programming language

role DLElem[::T] {
    has DLElem[T] $.prev is rw;
    has DLElem[T] $.next is rw;
    has T $.payload = T;

    method pre-insert(T $payload) {
	die "Can't insert before beginning" unless $!prev;
	my $elem = ::?CLASS.new(:$payload);
	$!prev.next = $elem;
	$elem.prev = $!prev;
	$elem.next = self;
	$!prev = $elem;
	$elem;
    }

    method post-insert(T $payload) {
	die "Can't insert after end" unless $!next;
	my $elem = ::?CLASS.new(:$payload);
	$!next.prev = $elem;
	$elem.next = $!next;
	$elem.prev = self;
	$!next = $elem;
	$elem;
    }

    method delete {
	die "Can't delete a sentinel" unless $!prev and $!next;
	$!next.prev = $!prev;
	$!prev.next = $!next;	# conveniently returns next element
    }
}

role DLList[::DLE] {
    has DLE $.first;
    has DLE $.last;

    submethod BUILD {
	$!first = DLE.new;
	$!last = DLE.new;
	$!first.next = $!last;
	$!last.prev = $!first;
    }

    method list { ($!first.next, *.next ...^ !*.next).map: *.payload }
    method reverse { ($!last.prev, *.prev ...^ !*.prev).map: *.payload }
}

class DLElem_Int does DLElem[Int] {}
class DLList_Int does DLList[DLElem_Int] {}

my $dll = DLList_Int.new;

$dll.first.post-insert(1).post-insert(2).post-insert(3);
$dll.first.post-insert(0);

$dll.last.pre-insert(41).pre-insert(40).prev.delete;  # (deletes 3)
$dll.last.pre-insert(42);

say $dll.list;     # 0 1 2 40 41 42
say $dll.reverse;  # 42 41 40 2 1 0


  

You may also check:How to resolve the algorithm Literals/Floating point step by step in the Groovy programming language
You may also check:How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Integer sequence step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Voronoi diagram step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the Lasso programming language