aboutsummaryrefslogtreecommitdiff
path: root/challenge-068/athanasius/perl/SinglyLinkedList.pm
blob: f1157ad61f6f2ceb00ccddc88b9837e52090123c (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!perl

################################################################################
=comment

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

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

=cut
################################################################################

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

#===============================================================================
package SinglyLinkedList;
#===============================================================================

use strict;
use warnings;

#-------------------------------------------------------------------------------
sub new
#-------------------------------------------------------------------------------
{
    my ($class, @args) = @_;

    my  $self = { head => undef };
    my  $curr;

    for my $arg (@args)
    {
        my $node = { datum => $arg, next => undef };

        if (defined $curr)
        {
            $curr = $curr->{next} = $node;
        }
        else
        {
            $curr = $self->{head} = $node;
        }
    }

    return bless $self, $class;
}

#-------------------------------------------------------------------------------
sub get_head
#-------------------------------------------------------------------------------
{
    my ($self) = @_;

    return $self->{head};
}

#-------------------------------------------------------------------------------
sub remove_tail
#-------------------------------------------------------------------------------
{
    my ($self) = @_;
    my  $tail;

    if (my $curr = $self->{head})
    {
        if ($curr->{next})
        {
            $curr = $curr->{next} while $curr->{next} && $curr->{next}{next};
            $tail = $curr->{next};
            $curr->{next} = undef;
        }
        else
        {
            $tail = $curr;
            $self->{head} = undef;
        }
    }

    return $tail;
}

#-------------------------------------------------------------------------------
sub insert
#-------------------------------------------------------------------------------
{
    my ($self, $curr, $node_to_add) = @_;
    my  $old_next;

    if (defined $self->{head} && defined $curr && $curr)
    {
        $old_next     = $curr->{next};
        $curr->{next} = $node_to_add;
    }
    else
    {
        $old_next     = $self->{head};
        $self->{head} = $node_to_add;
    }

    $node_to_add->{next} = $old_next;
}

#-------------------------------------------------------------------------------
sub print
#-------------------------------------------------------------------------------
{
    my ($self, $title) = @_;

    print $title if defined $title;

    if (my $curr = $self->{head})
    {
        while ($curr)
        {
            print   $curr->{datum};
            $curr = $curr->{next};

            print ' -> ' if defined $curr;
        }
    }
    else
    {
        print '<empty>';
    }

    print "\n";
}

################################################################################
1;
################################################################################