aboutsummaryrefslogtreecommitdiff
path: root/challenge-059
diff options
context:
space:
mode:
authorFung Cheok Yin <61836418+E7-87-83@users.noreply.github.com>2020-05-08 20:20:53 +0800
committerGitHub <noreply@github.com>2020-05-08 20:20:53 +0800
commitd57751d69fca329eda59f26c2d7c12d486e16cfa (patch)
tree6bd1fe2b15aa3dc8f5ca9243b74136f99a1e0c3c /challenge-059
parent188b91e6f01090c705be44e0e3f50f406e0b4b77 (diff)
downloadperlweeklychallenge-club-d57751d69fca329eda59f26c2d7c12d486e16cfa.tar.gz
perlweeklychallenge-club-d57751d69fca329eda59f26c2d7c12d486e16cfa.tar.bz2
perlweeklychallenge-club-d57751d69fca329eda59f26c2d7c12d486e16cfa.zip
Add files via upload
Diffstat (limited to 'challenge-059')
-rw-r--r--challenge-059/cheok-yin-fung/perl/ch-1.pl121
1 files changed, 121 insertions, 0 deletions
diff --git a/challenge-059/cheok-yin-fung/perl/ch-1.pl b/challenge-059/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..64e34adbb2
--- /dev/null
+++ b/challenge-059/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,121 @@
+#!/usr/bin/perl
+use strict;
+
+{ package item;
+
+ sub new
+ {
+ my ($class) = @_;
+ bless {
+ _value => $_[1],
+ _next => $_[2],
+ }, $class;
+ }
+
+ sub value { $_[0]->{_value}}
+
+ sub inext {
+ my ($self, $newnext) = @_;
+ $self->{_next} = $newnext if defined($newnext);
+ return $self->{_next};
+ }
+
+ sub unlink {
+ my ($self) = @_;
+ $self->{_next} = undef;
+ return $self->{_next};
+ }
+}
+
+#my @input = (2,1, 2,3);
+#my $K = 2;
+
+my @input = (5,6,3,2,7,9);
+my $K = 6;
+
+#my @input = (1 , 4, 3, 2, 5, 2 );
+#my $K = 3;
+
+# my @input = (2 , 4, 3, 1, 5, 1 );
+ # my $K = 3;
+
+my ($head, $smallpivot, $inusepivot);
+my $ll;
+
+
+sub make_linked_list {
+ my @temp_list = @_;
+ $ll->[$#temp_list] = item->new( $temp_list[$#temp_list] , \(undef) );
+ for (reverse 0..$#temp_list-1) {
+ $ll->[$_] = item->new($temp_list[$_] , \( $ll->[$_+1] ) );
+ }
+ $head = \($ll->[0]);
+}
+
+
+
+my $HELPER = -1;
+make_linked_list($HELPER, @input);
+
+$smallpivot = $head;
+$inusepivot = ${$head}->inext;
+
+sub print_linked_listi { #for testing use
+ my $i = 0;
+ my $t = ${$head};
+ while( $t = ${$t->inext} and $i<10) {
+ print $t->value, "->";
+ $i++;
+ }
+ print "nil\n";
+}
+
+sub print_linked_list {
+ my $t = ${$head};
+ while( $t = ${$t->inext} ) {
+ print $t->value, "->";
+ }
+ print "nil\n";
+}
+
+
+print "\nbefore: ";
+print_linked_list;
+print "\n";
+
+my $j = 0;
+
+my $previous = $head;
+while( 1) { #j is for testing help
+
+ # print $$inusepivot->value, " " ,$$smallpivot->value, " \n";
+ # print_linked_list;print "\n";
+
+ if ($$inusepivot->value < $K) {
+ $$previous->unlink;
+ $$previous->inext( $$inusepivot->inext );
+
+ $$inusepivot->unlink;
+ $$inusepivot->inext($$smallpivot->inext);
+
+ $$smallpivot->unlink;
+ $$smallpivot->inext($inusepivot);
+ $smallpivot = $inusepivot;
+
+ }
+
+
+ if (($$previous->inext != \undef) and ($$inusepivot->inext != \undef)) {
+ $previous = $inusepivot;
+ $inusepivot = $$inusepivot->inext;
+ }
+ else {
+ print "\nafter : ";
+ print_linked_list;
+ exit;
+ }
+
+}
+
+
+