diff --git a/exercises/SinglyLinkedList/Complete/NodeComplete.php b/exercises/SinglyLinkedList/Complete/NodeComplete.php new file mode 100644 index 0000000..3472f33 --- /dev/null +++ b/exercises/SinglyLinkedList/Complete/NodeComplete.php @@ -0,0 +1,31 @@ +data = $data; + } + + public function setLink(?NodeComplete $link): void + { + $this->link = $link; + } + + public function getLink(): ?NodeComplete + { + return $this->link; + } + + public function getData(): string + { + return $this->data; + } +} diff --git a/exercises/SinglyLinkedList/Complete/SinglyLinkedListComplete.php b/exercises/SinglyLinkedList/Complete/SinglyLinkedListComplete.php new file mode 100644 index 0000000..2e673a7 --- /dev/null +++ b/exercises/SinglyLinkedList/Complete/SinglyLinkedListComplete.php @@ -0,0 +1,50 @@ +head) { + $this->head = $node; + } + if ($this->last instanceof NodeComplete) { + $this->last->setLink($node); + } + + $this->last = $node; + } + + public function reverse(): void + { + $current = $this->head; + $new = null; + + while (null !== $current) { + $temp = $current->getLink(); + $current->setLink($new); + $new = $current; + $current = $temp; + } + $this->head = $new; + } + + public function print(): string + { + $currentNode = $this->head; + $output = ''; + + while (null !== $currentNode) { + $output .= $currentNode->getData(); + $currentNode = $currentNode->getLink(); + } + return $output; + } +} diff --git a/exercises/SinglyLinkedList/Node.php b/exercises/SinglyLinkedList/Node.php new file mode 100644 index 0000000..b0b961c --- /dev/null +++ b/exercises/SinglyLinkedList/Node.php @@ -0,0 +1,27 @@ +setLink($linkedNode); + * + * $node->getData() -> 'first' + * $node->getLink() -> $linkedNode +*/ + +final class Node +{ +} diff --git a/exercises/SinglyLinkedList/SinglyLinkedList.php b/exercises/SinglyLinkedList/SinglyLinkedList.php new file mode 100644 index 0000000..644e036 --- /dev/null +++ b/exercises/SinglyLinkedList/SinglyLinkedList.php @@ -0,0 +1,31 @@ +add(new Node('a')); + * $list->add(new Node('b')); + * $list->add(new Node('c')); + * + * $list::print() -> abc + * + * $list->reverse(); + * $list->print() -> cba + * +*/ +final class SinglyLinkedList +{ +} diff --git a/tests/SinglyLinkedList/Complete/SinglyLinkedListCompleteTest.php b/tests/SinglyLinkedList/Complete/SinglyLinkedListCompleteTest.php new file mode 100644 index 0000000..8782d5e --- /dev/null +++ b/tests/SinglyLinkedList/Complete/SinglyLinkedListCompleteTest.php @@ -0,0 +1,54 @@ +list = new SinglyLinkedListComplete(); + } + + public function testHasMethods(): void + { + self::markTestSkipped(); + self::assertTrue( + method_exists(SinglyLinkedListComplete::class, 'add'), + 'Class does not have method add' + ); + self::assertTrue( + method_exists(SinglyLinkedListComplete::class, 'reverse'), + 'Class does not have method reverse' + ); + self::assertTrue( + method_exists(SinglyLinkedListComplete::class, 'print'), + 'Class does not have method print' + ); + } + + public function testPrint(): void + { + $this->list->add(new NodeComplete("a")); + $this->list->add(new NodeComplete("b")); + $this->list->add(new NodeComplete("c")); + + self::assertSame('abc', $this->list->print()); + } + + public function testReverse(): void + { + $this->list->add(new NodeComplete("a")); + $this->list->add(new NodeComplete("b")); + $this->list->add(new NodeComplete("c")); + $this->list->reverse(); + self::assertSame('cba', $this->list->print()); + } +} diff --git a/tests/SinglyLinkedList/SinglyLinkedListTest.php b/tests/SinglyLinkedList/SinglyLinkedListTest.php new file mode 100644 index 0000000..541e52b --- /dev/null +++ b/tests/SinglyLinkedList/SinglyLinkedListTest.php @@ -0,0 +1,57 @@ +list = new SinglyLinkedList(); + } + + public function testHasMethods(): void + { + self::markTestSkipped(); + self::assertTrue( + method_exists(SinglyLinkedList::class, 'add'), + 'Class does not have method add' + ); + self::assertTrue( + method_exists(SinglyLinkedList::class, 'reverse'), + 'Class does not have method reverse' + ); + self::assertTrue( + method_exists(SinglyLinkedList::class, 'print'), + 'Class does not have method print' + ); + } + + public function testPrint(): void + { + self::markTestSkipped(); + $this->list->add(new Node("a")); + $this->list->add(new Node("b")); + $this->list->add(new Node("c")); + self::assertSame('abc', $this->list->print()); + } + + public function testReverse(): void + { + self::markTestSkipped(); + $this->list = new SinglyLinkedList; + $this->list->add(new Node("a")); + $this->list->add(new Node("b")); + $this->list->add(new Node("c")); + + $this->list->reverse(); + self::assertSame('cba', $this->list->print()); + } +}