Skip to content

Commit d16216e

Browse files
committed
fix linked list remove method
1 parent cf1bb17 commit d16216e

File tree

4 files changed

+83
-33
lines changed

4 files changed

+83
-33
lines changed

src/Common/Abstracts/AbstractLinkedList.php

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* MIT License
45
*
@@ -46,6 +47,7 @@
4647
*
4748
*/
4849
abstract class AbstractLinkedList implements IComparable, JsonSerializable {
50+
4951
/** @var Node */
5052
private $head = null;
5153

@@ -74,14 +76,14 @@ abstract class AbstractLinkedList implements IComparable, JsonSerializable {
7476
* 5 -> 4 -> 3 -> 2 -> 1 -> NULL
7577
*/
7678
public function reverse() {
77-
$prev = null;
78-
$next = null;
79+
$prev = null;
80+
$next = null;
7981
$current = $this->getHead();
8082

8183
while ($current !== null) {
8284
$next = $current->getNext();
8385
$current->setNext($prev);
84-
$prev = $current;
86+
$prev = $current;
8587
$current = $next;
8688
}
8789
$this->setHead($prev);
@@ -182,10 +184,10 @@ public function removeDuplicates() {
182184
*
183185
*/
184186
public function getLastElements(int $number): AbstractLinkedList {
185-
$p1 = $this->getHead();
186-
$p2 = $this->getHead();
187+
$p1 = $this->getHead();
188+
$p2 = $this->getHead();
187189
$number = $number > $this->head->size() ? $this->head->size() : $number;
188-
$list = $this->getEmptyInstance();
190+
$list = $this->getEmptyInstance();
189191

190192
for ($i = 0; $i < $number; $i++) {
191193
if ($p1 == null) {
@@ -252,8 +254,8 @@ public function getFirstElements(int $number): AbstractLinkedList {
252254
$head = $this->getHead();
253255
//if there are more elements requested than the list provides
254256
$number = $number > $head->size() ? $head->size() : $number;
255-
$list = $this->getEmptyInstance();
256-
$i = 0;
257+
$list = $this->getEmptyInstance();
258+
$i = 0;
257259
while ($i < $number) {
258260
//TODO append or prepend?
259261
$list->append($head);
@@ -402,12 +404,20 @@ public function containsKey($key): bool {
402404
*/
403405
public function remove($key): bool {
404406
/** @var Node $previous */
405-
$previous = $head = $this->getHead();
407+
$head = $this->getHead();
408+
$previous = null;
406409
if ($head === null) {
407410
return true;
408411
}
412+
413+
if (Comparator::equals($head->getKey(), $key)) {
414+
$this->setHead(
415+
$head->getNext()
416+
);
417+
return true;
418+
}
419+
409420
$i = 1;
410-
$headSize = $head->size();
411421

412422
/*
413423
* The while loop iterates over all nodes until the
@@ -428,10 +438,13 @@ public function remove($key): bool {
428438
* after that one who should be deleted.
429439
*/
430440
$previous = $head;
431-
$head = $head->getNext();
441+
$head = $head->getNext();
432442
$i++;
433443
}
434444

445+
if (null === $head) {
446+
return false;
447+
}
435448
/*
436449
* If the value that should be deleted is not in the list,
437450
* this set instruction assigns the next node to the actual.
@@ -440,10 +453,8 @@ public function remove($key): bool {
440453
* assigned to the previous node of the node that
441454
* should be deleted (if there is a node present).
442455
*/
443-
if ($head !== null) {
444-
$previous->setNext($head->getNext());
445-
}
446-
return $i !== $headSize;
456+
$previous->setNext($head->getNext());
457+
return true;
447458
}
448459

449460
/**
@@ -455,7 +466,7 @@ public function remove($key): bool {
455466
*/
456467
public function replaceValue($key, $value): bool {
457468
$replaced = false;
458-
$node = $this->getHead();
469+
$node = $this->getHead();
459470
while ($node !== null) {
460471
if (Comparator::equals($node->getKey(), $key)) {
461472
$node->setValue($value);
@@ -486,7 +497,7 @@ public function compareTo($object): int {
486497
*/
487498
public function hasLoop(): bool {
488499
$tortoise = $this->getHead();
489-
$hare = $this->getHead();
500+
$hare = $this->getHead();
490501

491502
while ($tortoise !== null && $hare->getNext() !== null) {
492503
$hare = $hare->getNext()->getNext();
@@ -596,7 +607,7 @@ public function isEmpty() {
596607
/**
597608
* Specify data which should be serialized to JSON
598609
*
599-
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
610+
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
600611
* @return mixed data which can be serialized by <b>json_encode</b>,
601612
* which is a value of any type other than a resource.
602613
* @since 5.4.0

src/Datastructure/Table/HashTable.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* MIT License
45
*
@@ -50,6 +51,7 @@
5051
* @package doganoo\PHPAlgorithms\Maps
5152
*/
5253
class HashTable extends AbstractTable implements JsonSerializable {
54+
5355
/**
5456
* @var array $bucket the buckets containing the nodes
5557
*/
@@ -136,7 +138,7 @@ private function getBucketIndex($key) {
136138
*
137139
* Doing this avoids hash collisions.
138140
*/
139-
$hash = $this->getHash($key);
141+
$hash = $this->getHash($key);
140142
$arrayIndex = $this->getArrayIndex($hash);
141143
return $arrayIndex;
142144
}
@@ -152,7 +154,7 @@ private function getBucketIndex($key) {
152154
*/
153155
private function getHash($key): int {
154156
$key = MapUtil::normalizeKey($key);
155-
return crc32($key);
157+
return crc32((string) $key);
156158
}
157159

158160
/**
@@ -226,7 +228,7 @@ public function getNodeByKey($key): ?Node {
226228
public function size(): int {
227229
$size = 0;
228230
/**
229-
* @var string $hash
231+
* @var string $hash
230232
* @var AbstractLinkedList $list
231233
*/
232234
foreach ($this->bucket as $hash => $list) {
@@ -244,7 +246,7 @@ public function size(): int {
244246
public function containsValue($value): bool {
245247

246248
/**
247-
* @var string $arrayIndex
249+
* @var string $arrayIndex
248250
* @var SinglyLinkedList $list
249251
*/
250252
foreach ($this->bucket as $arrayIndex => $list) {
@@ -272,7 +274,7 @@ public function containsValue($value): bool {
272274
*/
273275
public function containsKey($key): bool {
274276
/**
275-
* @var string $arrayIndex
277+
* @var string $arrayIndex
276278
* @var SinglyLinkedList $list
277279
*/
278280
foreach ($this->bucket as $arrayIndex => $list) {
@@ -305,7 +307,7 @@ public function containsKey($key): bool {
305307
*/
306308
public function getNodeByValue($value): ?Node {
307309
/**
308-
* @var string $arrayIndex
310+
* @var string $arrayIndex
309311
* @var SinglyLinkedList $list
310312
*/
311313
foreach ($this->bucket as $arrayIndex => $list) {
@@ -364,11 +366,15 @@ public function remove($key): bool {
364366
* is no node to remove.
365367
*/
366368
if ($list->size() == 1 && $head->getKey() === $key) {
367-
//$this->bucket[$arrayIndex] = null;
368369
unset($this->bucket[$arrayIndex]);
369370
return true;
370371
}
371-
return $list->remove($key);
372+
$removed = $list->remove($key);
373+
echo $list->size();
374+
echo "\n";
375+
// $this->bucket[$arrayIndex] = $list;
376+
// var_dump($this->bucket[$arrayIndex]);
377+
return $removed;
372378
}
373379

374380
/**
@@ -397,7 +403,7 @@ public function keySet(): array {
397403
$head = $list->getHead();
398404
while ($head !== null) {
399405
$keySet[] = $head->getKey();
400-
$head = $head->getNext();
406+
$head = $head->getNext();
401407
}
402408
}
403409
return $keySet;
@@ -407,7 +413,7 @@ public function keySet(): array {
407413
* @return array
408414
*/
409415
public function countPerBucket() {
410-
$i = 0;
416+
$i = 0;
411417
$array = [];
412418
/** @var SinglyLinkedList $list */
413419
foreach ($this->bucket as $list) {
@@ -421,15 +427,16 @@ public function countPerBucket() {
421427
/**
422428
* Specify data which should be serialized to JSON
423429
*
424-
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
430+
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
425431
* @return mixed data which can be serialized by <b>json_encode</b>,
426432
* which is a value of any type other than a resource.
427433
* @since 5.4.0
428434
*/
429435
public function jsonSerialize() {
430436
return [
431-
"buckets" => $this->bucket
437+
"buckets" => $this->bucket
432438
, "max_size" => $this->maxSize,
433439
];
434440
}
441+
435442
}

tests/Lists/LinkedLists/SinglyLinkedListTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
namespace doganoo\PHPAlgorithmsTest\Lists\LinkedLists;
2828

29+
use doganoo\PHPAlgorithms\Datastructure\Lists\LinkedLists\SinglyLinkedList;
2930
use doganoo\PHPAlgorithmsTest\Util\LinkedListUtil;
3031
use PHPUnit\Framework\TestCase;
3132

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

76+
public function testRemove() {
77+
$singlyLinkedList = new SinglyLinkedList();
78+
$singlyLinkedList->add("calorie_tracker", new class {
79+
80+
});
81+
$singlyLinkedList->add("tnc", new class {
82+
83+
});
84+
85+
$this->assertTrue(2 === $singlyLinkedList->size());
86+
$singlyLinkedList->remove("calorie_tracker");
87+
$this->assertTrue(1 === $singlyLinkedList->size());
88+
89+
}
90+
7591
}

tests/Table/HashTableTest.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,25 @@ public function testGetNodeByValue() {
8383
* tests removing a value from the map
8484
*/
8585
public function testRemove() {
86-
$hashMap = HashTableUtil::getHashTable(500);
87-
$boolean = $hashMap->remove(320);
88-
$this->assertTrue($boolean);
86+
$hashTable = new HashTable();
87+
$hashTable->put("about", new class{});
88+
$hashTable->put("account", new class{});
89+
$hashTable->put("apps", new class{});
90+
$hashTable->put("calorie_tracker", new class{});
91+
$hashTable->put("tnc", new class{});
92+
$hashTable->put("forgot_password", new class{});
93+
$hashTable->put("general_api", new class{});
94+
$hashTable->put("install", new class{});
95+
$hashTable->put("login", new class{});
96+
$hashTable->put("logout", new class{});
97+
$hashTable->put("maintenance", new class{});
98+
$hashTable->put("password_manager", new class{});
99+
$hashTable->put("promotion", new class{});
100+
$hashTable->put("register", new class{});
101+
$hashTable->put("users", new class{});
102+
$this->assertTrue(null !== $hashTable->get("calorie_tracker"));
103+
$hashTable->remove("calorie_tracker");
104+
$this->assertTrue(null === $hashTable->get("calorie_tracker"));
89105
}
90106

91107
/**

0 commit comments

Comments
 (0)