Skip to content

Commit f4b5b3f

Browse files
committed
Initial commit
1 parent c1ea5fb commit f4b5b3f

7 files changed

Lines changed: 213 additions & 37 deletions

File tree

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ Linked List implementation in PHP.
88
composer require angle/linked
99
```
1010

11+
## Usage
12+
13+
Creating an empty list and appending items.
14+
```php
15+
$list = new Angle\Linked\Linked;
16+
$list->append('PHP');
17+
$list->append('Ruby');
18+
$list->append('Javascript');
19+
20+
print $list->head->next->next->data; // Javascript
21+
```
22+
23+
Prepend elements to the list:
24+
```php
25+
$list->prepend('Golang');
26+
27+
print $list->head->data; // Golang
28+
```
29+
30+
Print the list:
31+
```php
32+
print $list; // Golang, PHP, Ruby, Javascript
33+
```
34+
1135
## Contributing
1236

1337
Improvements are welcome! Feel free to submit pull requests.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angle/linked",
3-
"description": "Composer Package Template",
3+
"description": "Linked List implementation in PHP.",
44
"type": "library",
55
"license": "MIT",
66
"authors": [

src/Linked.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Angle\Linked;
4+
5+
use Angle\Linked\Node;
6+
7+
class Linked
8+
{
9+
/** @var Node */
10+
public $head = null;
11+
12+
/**
13+
* Creates a list with a give set of nodes.
14+
* @param array $nodes
15+
*/
16+
public function __construct(...$nodes)
17+
{
18+
foreach ($nodes as $node) {
19+
$this->append($node);
20+
}
21+
}
22+
23+
/**
24+
* Appends a node to the linked list.
25+
* @param mixed $data
26+
*/
27+
public function append($data): void
28+
{
29+
if ($this->head == null) {
30+
$this->head = new Node($data);
31+
return;
32+
}
33+
34+
$current = $this->head;
35+
36+
while ($current->next != null) {
37+
$current = $current->next;
38+
}
39+
40+
$current->next = new Node($data);
41+
}
42+
43+
/**
44+
* Prepends a node to the head of the list.
45+
* @param mixed $data
46+
*/
47+
public function prepend($data): void
48+
{
49+
$newHead = new Node($data);
50+
$newHead->next = $this->head;
51+
$this->head = $newHead;
52+
}
53+
54+
/**
55+
* Deletes a node matching the given data.
56+
* @param mixed $data
57+
*/
58+
public function delete($data): void
59+
{
60+
if ($this->head == null) {
61+
return;
62+
}
63+
64+
if ($this->head->data == $data) {
65+
$this->head = $this->head->next;
66+
return;
67+
}
68+
69+
$current = $this->head;
70+
71+
while ($current->next != null) {
72+
if ($current->next->data == $data) {
73+
$current->next = $current->next->next;
74+
return;
75+
}
76+
77+
$current = $current->next;
78+
}
79+
}
80+
81+
public function __toString()
82+
{
83+
$current = $this->head;
84+
$string = '';
85+
86+
while ($current != null) {
87+
$string .= $current->data;
88+
89+
if ($current->next != null) {
90+
$string .= ', ';
91+
}
92+
93+
$current = $current->next;
94+
}
95+
96+
return $string;
97+
}
98+
}

src/Node.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Angle\Linked;
4+
5+
class Node
6+
{
7+
/** @var Node */
8+
public $next;
9+
10+
/** @var mixed */
11+
public $data;
12+
13+
/** @param mixed $data */
14+
public function __construct($data)
15+
{
16+
$this->data = $data;
17+
}
18+
}

src/Package.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/LinkedTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
use Angle\Linked\Linked;
6+
use Angle\Linked\Node;
7+
8+
class LinkedTest extends TestCase
9+
{
10+
/** @var Linked */
11+
public $list;
12+
13+
public function setUp() : void
14+
{
15+
$this->list = new Linked;
16+
$this->list->append('PHP');
17+
$this->list->append('Ruby');
18+
$this->list->append('Javascript');
19+
}
20+
21+
/** @test */
22+
public function canCreateSingleNode()
23+
{
24+
$item = new Node('I <3 programming.');
25+
26+
$this->assertTrue($item->data === 'I <3 programming.');
27+
}
28+
29+
/** @test */
30+
public function nodesWereAppended()
31+
{
32+
$this->assertTrue($this->list->head->next->next->data == 'Javascript');
33+
}
34+
35+
/** @test */
36+
public function nodesCanBePrepended()
37+
{
38+
$this->list->prepend('Golang');
39+
40+
$this->assertTrue($this->list->head->data == 'Golang');
41+
}
42+
43+
/** @test */
44+
public function listCanBeConvertedToString()
45+
{
46+
$expected = 'Golang, PHP, Ruby, Javascript';
47+
48+
$this->list->prepend('Golang');
49+
50+
print $this->list;
51+
52+
$this->expectOutputString($expected);
53+
54+
$this->assertTrue($expected == (string) $this->list);
55+
}
56+
57+
/** @test */
58+
public function listCanBeCreatedWithNodes()
59+
{
60+
$list = new Linked('Laravel', 'Symfony', 'Rails');
61+
62+
$this->assertTrue($list->head->next->next->data == 'Rails');
63+
}
64+
65+
/** @test */
66+
public function aListCanContainAnything()
67+
{
68+
$list = new Linked(['some' => 'array'], 42, new stdClass);
69+
70+
$this->assertTrue($list->head->next->data == 42);
71+
}
72+
}

tests/Test.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)