Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exercise for a singly linked list #69

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions exercises/SinglyLinkedList/Complete/NodeComplete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

namespace Exercises\SinglyLinkedList\Complete;

final class NodeComplete {

private $data;

private $link = null;

public function __construct(string $data)
{
$this->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;
}
}
50 changes: 50 additions & 0 deletions exercises/SinglyLinkedList/Complete/SinglyLinkedListComplete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);

namespace Exercises\SinglyLinkedList\Complete;

use Exercises\SinglyLinkedList\Complete\NodeComplete;

final class SinglyLinkedListComplete
{
private $head = null;
private $last = null;

public function add(NodeComplete $node): void
{
if (null === $this->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;
}
}
27 changes: 27 additions & 0 deletions exercises/SinglyLinkedList/Node.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);

namespace Exercises\SinglyLinkedList;

/**
* Create a Node.
* The node can have a linked node and a data string.
*
* @property string $data
* @property Node|null $link
*
* @method void setLink(Node $link)
* @method Node|null getLink()
* @method string getData()

* @example $node = new Node('first');
* $linkedNode = new Node('second');
* $node->setLink($linkedNode);
*
* $node->getData() -> 'first'
* $node->getLink() -> $linkedNode
*/

final class Node
{
}
31 changes: 31 additions & 0 deletions exercises/SinglyLinkedList/SinglyLinkedList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

namespace Exercises\SinglyLinkedList;

/**
* Implement a singly linked-list
* Each element only knows about the next Node, but not the previous.
* Implement a print method, that prints all Node's data in their order.
* Implement a reverse method that reverses the order of the linked list.
*
* @property Node|null $head
* @property Node|null $last
* @method void add(Node $link)
* @method void reverse()
* @method string print()

* @example $list = new SinglyLinkedList;
* $list->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
{
}
54 changes: 54 additions & 0 deletions tests/SinglyLinkedList/Complete/SinglyLinkedListCompleteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Tests\SinglyLinkedList;

use Exercises\SinglyLinkedList\Complete\SinglyLinkedListComplete;
use Exercises\SinglyLinkedList\Complete\NodeComplete;
use PHPUnit\Framework\TestCase;

final class SinglyLinkedListCompleteTest extends TestCase
{
private $list;

protected function setUp(): void
{
$this->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());
}
}
57 changes: 57 additions & 0 deletions tests/SinglyLinkedList/SinglyLinkedListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Tests\SinglyLinkedList;

use Exercises\SinglyLinkedList\Node;
use Exercises\SinglyLinkedList\SinglyLinkedList;
use PHPUnit\Framework\TestCase;

final class SinglyLinkedListTest extends TestCase
{
private $list;

protected function setUp(): void
{
$this->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());
}
}