diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2021-01-31 11:19:17 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2021-01-31 11:19:17 +0800 |
| commit | 9c8eba9106a3b8a09e0f75e0ff18e9519dfbd02c (patch) | |
| tree | 68d298e0d0dd5e36ff8095e58b6f41aeb40abc0d /challenge-068 | |
| parent | d1ebec467d1701aa58ca587368dea6072f72268d (diff) | |
| download | perlweeklychallenge-club-9c8eba9106a3b8a09e0f75e0ff18e9519dfbd02c.tar.gz perlweeklychallenge-club-9c8eba9106a3b8a09e0f75e0ff18e9519dfbd02c.tar.bz2 perlweeklychallenge-club-9c8eba9106a3b8a09e0f75e0ff18e9519dfbd02c.zip | |
4 Smalltalk scripts, 2 Perl scripts
Diffstat (limited to 'challenge-068')
| -rw-r--r-- | challenge-068/cheok-yin-fung/smalltalk/ch-2.st | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/challenge-068/cheok-yin-fung/smalltalk/ch-2.st b/challenge-068/cheok-yin-fung/smalltalk/ch-2.st new file mode 100644 index 0000000000..f4010ea6b6 --- /dev/null +++ b/challenge-068/cheok-yin-fung/smalltalk/ch-2.st @@ -0,0 +1,121 @@ +"GNU Smalltalk 3.2.5" +"The Weekly Challenge 068 Task 2" +"Reorder List" +"Usage: gst -S ch-2.st" +"written on 30th Jan 2021" +"Smalltalk has built-in Linked List, +here is reventing the wheel." + + +Object subclass: Node [ + |value next| + Node class >> value: v next: n [ + <category: 'instance creation'> + ^(super new) setValue: v setNext: n + ] + value [ + ^value + ] + + setValue: v setNext: n [ + value := v. + next := n. + ^self + ] + + setValue: v [ + <category: 'basic'> + value := v. + ^self + ] + next [ + ^next + ] + setNext: n [ + <category: 'basic'> + next := n. + ^next + ] + setNextnil [ + next := nil. + ] +] + +Object subclass: SLL [ + |root| + SLL class >> root: node [ + <category: 'instance creation'> + ^(super new) setRoot: node + ] + root [ + ^root + ] + setRoot: node [ + <category: 'basic'> + root := node. + ] + showList [ + |myNode| + myNode := root. + [myNode next isNil not] whileTrue:[ + (myNode value displayString, ' ' ) print. + myNode := myNode next. + ]. + myNode value printNl. + ] +] + +i := Node value: 23 next: nil. +h := Node value: 19 next: i. +g := Node value: 17 next: h. +f := Node value: 13 next: g. +e := Node value: 11 next: f. +d := Node value: 7 next: e. +c := Node value: 5 next: d. +b := Node value: 3 next: c. +a := Node value: 2 next: b. + +abc := SLL root: a. + +'Before operation' printNl. +abc showList. + +listsize := 1. + +traveller := abc root. +[traveller next isNil not] whileTrue:[ + listsize := listsize + 1. + traveller := traveller next. +]. +listTail := traveller. + + +nodeHandle := abc root. + +(listsize - 1 // 2) timesRepeat: [ + traveller := abc root. + [( traveller next == listTail ) not ] whileTrue: [ + traveller := traveller next. + ]. + preListTail := traveller. + + waiter := nodeHandle next. + preListTail setNextnil. + nodeHandle setNextnil. + guestSeat := listTail. + nodeHandle setNext: guestSeat. + listTail := preListTail. + guestSeat setNext: waiter. + + (nodeHandle next isNil) ifFalse: [ + (nodeHandle next isNil not) ifTrue: + [nodeHandle := nodeHandle next next.] + ]. +]. + +'After operation' printNl. + +abc showList. + +ObjectMemory quit. + |
