aboutsummaryrefslogtreecommitdiff
path: root/challenge-068/athanasius/raku/SinglyLinkedList.rakumod
blob: 7169b2f4e404f3e663d2b3982bbe8ca1df3acdc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use v6d;

################################################################################
=begin comment

Perl Weekly Challenge 068
=========================

Task #2
-------
*Reorder List*

=end comment
################################################################################

#--------------------------------------#
# Copyright © 2020 PerlMonk Athanasius #
#--------------------------------------#

#===============================================================================
unit class SinglyLinkedList;
#===============================================================================

has $.head = Nil;

#-------------------------------------------------------------------------------
method append(Str:D $element)
#-------------------------------------------------------------------------------
{
    my %node = datum => $element, next => Nil;

    if $!head
    {
        my Hash $current = $!head;

        $current = $current{'next'} while $current{'next'};

        $current{'next'} = %node;
    }
    else
    {
        $!head = %node;
    }
}

#-------------------------------------------------------------------------------
method remove-tail(--> Hash:D)
#-------------------------------------------------------------------------------
{
    my Hash $tail;
    my Hash $current = $!head;

    if $current
    {
        if $current{'next'}
        {
            $current = $current{'next'}
                 while $current{'next'} && $current{'next'}{'next'};

            $tail = $current{'next'};

            $current{'next'} = Nil;
        }
        else
        {
            $tail  = $current;
            $!head = Nil;
        }
    }

    return $tail;
}

#-------------------------------------------------------------------------------
method insert(Hash:D $current, Hash:D $node-to-add)
#-------------------------------------------------------------------------------
{
    my $old-next;

    if $!head && $current
    {
        $old-next        = $current{'next'};
        $current{'next'} = $node-to-add;
    }
    else
    {
        $old-next        = $!head;
        $!head           = $node-to-add;
    }

    $node-to-add{'next'} = $old-next;
}

#-------------------------------------------------------------------------------
method print(Str:D $title)
#-------------------------------------------------------------------------------
{
    $title.print if $title;

    if $!head
    {
        my $current = $!head;

        while $current
        {
            $current{'datum'}.print;
            $current = $current{'next'};
           ($current ?? ' -> ' !! "\n").print;
        }
    }
    else
    {
        '<empty>'.put;
    }
}

################################################################################