Merge branch 'TheAlgorithms:master' into problem8

This commit is contained in:
Ruben del Blanco Rey 2023-04-21 12:32:29 +02:00 committed by GitHub
commit 2c80872478
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 187 additions and 0 deletions

View File

@ -13,6 +13,9 @@
* [Octaltodecimal](./Conversions/OctalToDecimal.php)
* [Speedconversion](./Conversions/SpeedConversion.php)
## Datastructures
* [Singlylinkedlist](./DataStructures/SinglyLinkedList.php)
## Graphs
* [Breadthfirstsearch](./Graphs/BreadthFirstSearch.php)
* [Depthfirstsearch](./Graphs/DepthFirstSearch.php)
@ -84,6 +87,8 @@
* [Morsecodetest](./tests/Ciphers/MorseCodeTest.php)
* Conversions
* [Conversionstest](./tests/Conversions/ConversionsTest.php)
* Datastructures
* [Singlylinkedlisttest](./tests/DataStructures/SinglyLinkedListTest.php)
* Graphs
* [Breadthfirstsearchtest](./tests/Graphs/BreadthFirstSearchTest.php)
* [Depthfirstsearchtest](./tests/Graphs/DepthFirstSearchTest.php)

View File

@ -0,0 +1,47 @@
<?php
/**
* Singly Linked List
*/
class SinglyLinkedList
{
public ?SinglyLinkedList $next = null;
public $data;
public function __construct($data)
{
$this->data = $data;
}
public function append($data): void
{
$current = $this;
while ($current instanceof SinglyLinkedList && isset($current->next)) {
$current = $current->next;
}
$current->next = new SinglyLinkedList($data);
}
public function delete($data): SinglyLinkedList
{
$current = $this;
if ($current->data == $data) {
return $current->next;
}
while ($current instanceof SinglyLinkedList && isset($current->next)) {
if ($current->next->data === $data) {
$current->next = $current->next->next;
return $this;
}
$current = $current->next;
}
return $this;
}
}

View File

@ -0,0 +1,135 @@
<?php
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../DataStructures/SinglyLinkedList.php';
/**
* Tests for the Singly Linked Lists class
*/
class SinglyLinkedListTest extends TestCase
{
/**
* Create a SinglyLinkedList node
*/
private function createNode(string $string): SinglyLinkedList
{
$node = new SinglyLinkedList($string[0]);
for ($i = 1; $i < strlen($string); $i++) {
$node->append($string[$i]);
}
return $node;
}
/**
* Provider of SinglyLinkedList nodes
*/
public function provideNodes()
{
return [
'IsPalindrome' => [
'node' => $this->createNode('hannah'),
'expected' => true
],
'IsPalindrome2' => [
'node' => $this->createNode('hanah'),
'expected' => true
],
'IsNotPalindrome' => [
'node' => $this->createNode('hanbah'),
'expected' => false
],
'IsNotPalindrome2' => [
'node' => $this->createNode('han1h'),
'expected' => false
],
];
}
/**
* Test the if a SinglyLinkedList is a palindrome
*
* @dataProvider provideNodes
*/
public function testIsPalindrome($node, $expected): void
{
$this->assertEquals($expected, $this->isPalindrome($node));
}
/**
* Supporting method for testing if a linked list is palindrome
*/
private function isPalindrome(SinglyLinkedList $node): bool
{
$length = $this->length($node);
$pairs = 0;
$curr = $node;
while ($curr instanceof SinglyLinkedList) {
if ($this->hasPair($curr->next, $curr->data)) {
$pairs++;
}
$curr = $curr->next;
}
return $pairs == floor($length / 2);
}
/**
* Supporting method for testing if a linked list is palindrome
*/
private function hasPair($node, string $data): bool
{
if (! $node instanceof SinglyLinkedList) {
return false;
}
$curr = $node;
while ($curr instanceof SinglyLinkedList) {
if ($curr->data == $data) {
return true;
}
$curr = $curr->next;
}
return false;
}
/**
* Supporting method for testing if a linked list is palindrome
*/
private function length(SinglyLinkedList $node): int
{
$curr = $node;
$counter = 0;
while ($curr instanceof SinglyLinkedList) {
$counter++;
$curr = $curr->next;
}
return $counter;
}
/**
* Test SinglyLinkedList's delete functionality
*/
public function testDelete(): void
{
$this->assertEquals(
$this->createNode('hanah'), // expected node
$this->createNode('hannah')->delete('n'), // actual node
);
$this->assertNotEquals(
$this->createNode('hanah'), // expected node
$this->createNode('hannah')->delete('h'), // actual node
);
}
}