Skip to content

Commit

Permalink
fix linked list remove method
Browse files Browse the repository at this point in the history
  • Loading branch information
doganoo committed Sep 28, 2019
1 parent cf1bb17 commit d16216e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 33 deletions.
47 changes: 29 additions & 18 deletions src/Common/Abstracts/AbstractLinkedList.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* MIT License
*
Expand Down Expand Up @@ -46,6 +47,7 @@
*
*/
abstract class AbstractLinkedList implements IComparable, JsonSerializable {

/** @var Node */
private $head = null;

Expand Down Expand Up @@ -74,14 +76,14 @@ abstract class AbstractLinkedList implements IComparable, JsonSerializable {
* 5 -> 4 -> 3 -> 2 -> 1 -> NULL
*/
public function reverse() {
$prev = null;
$next = null;
$prev = null;
$next = null;
$current = $this->getHead();

while ($current !== null) {
$next = $current->getNext();
$current->setNext($prev);
$prev = $current;
$prev = $current;
$current = $next;
}
$this->setHead($prev);
Expand Down Expand Up @@ -182,10 +184,10 @@ public function removeDuplicates() {
*
*/
public function getLastElements(int $number): AbstractLinkedList {
$p1 = $this->getHead();
$p2 = $this->getHead();
$p1 = $this->getHead();
$p2 = $this->getHead();
$number = $number > $this->head->size() ? $this->head->size() : $number;
$list = $this->getEmptyInstance();
$list = $this->getEmptyInstance();

for ($i = 0; $i < $number; $i++) {
if ($p1 == null) {
Expand Down Expand Up @@ -252,8 +254,8 @@ public function getFirstElements(int $number): AbstractLinkedList {
$head = $this->getHead();
//if there are more elements requested than the list provides
$number = $number > $head->size() ? $head->size() : $number;
$list = $this->getEmptyInstance();
$i = 0;
$list = $this->getEmptyInstance();
$i = 0;
while ($i < $number) {
//TODO append or prepend?
$list->append($head);
Expand Down Expand Up @@ -402,12 +404,20 @@ public function containsKey($key): bool {
*/
public function remove($key): bool {
/** @var Node $previous */
$previous = $head = $this->getHead();
$head = $this->getHead();
$previous = null;
if ($head === null) {
return true;
}

if (Comparator::equals($head->getKey(), $key)) {
$this->setHead(
$head->getNext()
);
return true;
}

$i = 1;
$headSize = $head->size();

/*
* The while loop iterates over all nodes until the
Expand All @@ -428,10 +438,13 @@ public function remove($key): bool {
* after that one who should be deleted.
*/
$previous = $head;
$head = $head->getNext();
$head = $head->getNext();
$i++;
}

if (null === $head) {
return false;
}
/*
* If the value that should be deleted is not in the list,
* this set instruction assigns the next node to the actual.
Expand All @@ -440,10 +453,8 @@ public function remove($key): bool {
* assigned to the previous node of the node that
* should be deleted (if there is a node present).
*/
if ($head !== null) {
$previous->setNext($head->getNext());
}
return $i !== $headSize;
$previous->setNext($head->getNext());
return true;
}

/**
Expand All @@ -455,7 +466,7 @@ public function remove($key): bool {
*/
public function replaceValue($key, $value): bool {
$replaced = false;
$node = $this->getHead();
$node = $this->getHead();
while ($node !== null) {
if (Comparator::equals($node->getKey(), $key)) {
$node->setValue($value);
Expand Down Expand Up @@ -486,7 +497,7 @@ public function compareTo($object): int {
*/
public function hasLoop(): bool {
$tortoise = $this->getHead();
$hare = $this->getHead();
$hare = $this->getHead();

while ($tortoise !== null && $hare->getNext() !== null) {
$hare = $hare->getNext()->getNext();
Expand Down Expand Up @@ -596,7 +607,7 @@ public function isEmpty() {
/**
* Specify data which should be serialized to JSON
*
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
Expand Down
31 changes: 19 additions & 12 deletions src/Datastructure/Table/HashTable.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* MIT License
*
Expand Down Expand Up @@ -50,6 +51,7 @@
* @package doganoo\PHPAlgorithms\Maps
*/
class HashTable extends AbstractTable implements JsonSerializable {

/**
* @var array $bucket the buckets containing the nodes
*/
Expand Down Expand Up @@ -136,7 +138,7 @@ private function getBucketIndex($key) {
*
* Doing this avoids hash collisions.
*/
$hash = $this->getHash($key);
$hash = $this->getHash($key);
$arrayIndex = $this->getArrayIndex($hash);
return $arrayIndex;
}
Expand All @@ -152,7 +154,7 @@ private function getBucketIndex($key) {
*/
private function getHash($key): int {
$key = MapUtil::normalizeKey($key);
return crc32($key);
return crc32((string) $key);
}

/**
Expand Down Expand Up @@ -226,7 +228,7 @@ public function getNodeByKey($key): ?Node {
public function size(): int {
$size = 0;
/**
* @var string $hash
* @var string $hash
* @var AbstractLinkedList $list
*/
foreach ($this->bucket as $hash => $list) {
Expand All @@ -244,7 +246,7 @@ public function size(): int {
public function containsValue($value): bool {

/**
* @var string $arrayIndex
* @var string $arrayIndex
* @var SinglyLinkedList $list
*/
foreach ($this->bucket as $arrayIndex => $list) {
Expand Down Expand Up @@ -272,7 +274,7 @@ public function containsValue($value): bool {
*/
public function containsKey($key): bool {
/**
* @var string $arrayIndex
* @var string $arrayIndex
* @var SinglyLinkedList $list
*/
foreach ($this->bucket as $arrayIndex => $list) {
Expand Down Expand Up @@ -305,7 +307,7 @@ public function containsKey($key): bool {
*/
public function getNodeByValue($value): ?Node {
/**
* @var string $arrayIndex
* @var string $arrayIndex
* @var SinglyLinkedList $list
*/
foreach ($this->bucket as $arrayIndex => $list) {
Expand Down Expand Up @@ -364,11 +366,15 @@ public function remove($key): bool {
* is no node to remove.
*/
if ($list->size() == 1 && $head->getKey() === $key) {
//$this->bucket[$arrayIndex] = null;
unset($this->bucket[$arrayIndex]);
return true;
}
return $list->remove($key);
$removed = $list->remove($key);
echo $list->size();
echo "\n";
// $this->bucket[$arrayIndex] = $list;
// var_dump($this->bucket[$arrayIndex]);
return $removed;
}

/**
Expand Down Expand Up @@ -397,7 +403,7 @@ public function keySet(): array {
$head = $list->getHead();
while ($head !== null) {
$keySet[] = $head->getKey();
$head = $head->getNext();
$head = $head->getNext();
}
}
return $keySet;
Expand All @@ -407,7 +413,7 @@ public function keySet(): array {
* @return array
*/
public function countPerBucket() {
$i = 0;
$i = 0;
$array = [];
/** @var SinglyLinkedList $list */
foreach ($this->bucket as $list) {
Expand All @@ -421,15 +427,16 @@ public function countPerBucket() {
/**
* Specify data which should be serialized to JSON
*
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize() {
return [
"buckets" => $this->bucket
"buckets" => $this->bucket
, "max_size" => $this->maxSize,
];
}

}
16 changes: 16 additions & 0 deletions tests/Lists/LinkedLists/SinglyLinkedListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace doganoo\PHPAlgorithmsTest\Lists\LinkedLists;

use doganoo\PHPAlgorithms\Datastructure\Lists\LinkedLists\SinglyLinkedList;
use doganoo\PHPAlgorithmsTest\Util\LinkedListUtil;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -72,4 +73,19 @@ public function testContains() {
$this->assertTrue($list->containsKey("test") === true);
}

public function testRemove() {
$singlyLinkedList = new SinglyLinkedList();
$singlyLinkedList->add("calorie_tracker", new class {

});
$singlyLinkedList->add("tnc", new class {

});

$this->assertTrue(2 === $singlyLinkedList->size());
$singlyLinkedList->remove("calorie_tracker");
$this->assertTrue(1 === $singlyLinkedList->size());

}

}
22 changes: 19 additions & 3 deletions tests/Table/HashTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,25 @@ public function testGetNodeByValue() {
* tests removing a value from the map
*/
public function testRemove() {
$hashMap = HashTableUtil::getHashTable(500);
$boolean = $hashMap->remove(320);
$this->assertTrue($boolean);
$hashTable = new HashTable();
$hashTable->put("about", new class{});
$hashTable->put("account", new class{});
$hashTable->put("apps", new class{});
$hashTable->put("calorie_tracker", new class{});
$hashTable->put("tnc", new class{});
$hashTable->put("forgot_password", new class{});
$hashTable->put("general_api", new class{});
$hashTable->put("install", new class{});
$hashTable->put("login", new class{});
$hashTable->put("logout", new class{});
$hashTable->put("maintenance", new class{});
$hashTable->put("password_manager", new class{});
$hashTable->put("promotion", new class{});
$hashTable->put("register", new class{});
$hashTable->put("users", new class{});
$this->assertTrue(null !== $hashTable->get("calorie_tracker"));
$hashTable->remove("calorie_tracker");
$this->assertTrue(null === $hashTable->get("calorie_tracker"));
}

/**
Expand Down

0 comments on commit d16216e

Please sign in to comment.