Skip to content

Commit aebb567

Browse files
authored
Linked list (#915)
1 parent 675a2da commit aebb567

5 files changed

Lines changed: 401 additions & 84 deletions

File tree

bin/auto-sync.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ knapsack
3939
largest-series-product
4040
leap
4141
list-ops
42+
linked-list
4243
luhn
4344
matching-brackets
4445
meetup

exercises/practice/linked-list/.meta/config.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"authors": [
33
"MichaelBunker"
44
],
5+
"contributors": [
6+
"A-O-Emmanuel"
7+
],
58
"files": {
69
"solution": [
710
"LinkedList.php"
@@ -13,5 +16,6 @@
1316
".meta/example.php"
1417
]
1518
},
16-
"blurb": "Implement a doubly linked list"
19+
"blurb": "Implement a doubly linked list.",
20+
"source": "Classic computer science topic"
1721
}

exercises/practice/linked-list/.meta/example.php

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

275
class LinkedList
@@ -71,6 +49,50 @@ public function shift()
7149

7250
return $data;
7351
}
52+
53+
public function count(): int
54+
{
55+
if (!$this->head) {
56+
return 0;
57+
}
58+
59+
$count = 1;
60+
$current = $this->head->next;
61+
62+
while ($current !== $this->head) {
63+
$count++;
64+
$current = $current->next;
65+
}
66+
67+
return $count;
68+
}
69+
70+
public function delete($value): void
71+
{
72+
if (!$this->head) {
73+
return;
74+
}
75+
76+
$current = $this->head;
77+
do {
78+
if ($current->data === $value) {
79+
if ($current->next === $current) {
80+
$this->head = null;
81+
return;
82+
}
83+
84+
$current->prev->next = $current->next;
85+
$current->next->prev = $current->prev;
86+
87+
if ($current === $this->head) {
88+
$this->head = $current->next;
89+
}
90+
91+
return;
92+
}
93+
$current = $current->next;
94+
} while ($current !== $this->head);
95+
}
7496
}
7597

7698
class Node
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[7f7e3987-b954-41b8-8084-99beca08752c]
13+
description = "pop gets element from the list"
14+
15+
[c3f67e5d-cfa2-4c3e-a18f-7ce999c3c885]
16+
description = "push/pop respectively add/remove at the end of the list"
17+
18+
[00ea24ce-4f5c-4432-abb4-cc6e85462657]
19+
description = "shift gets an element from the list"
20+
21+
[37962ee0-3324-4a29-b588-5a4c861e6564]
22+
description = "shift gets first element from the list"
23+
24+
[30a3586b-e9dc-43fb-9a73-2770cec2c718]
25+
description = "unshift adds element at start of the list"
26+
27+
[042f71e4-a8a7-4cf0-8953-7e4f3a21c42d]
28+
description = "pop, push, shift, and unshift can be used in any order"
29+
30+
[88f65c0c-4532-4093-8295-2384fb2f37df]
31+
description = "count an empty list"
32+
33+
[fc055689-5cbe-4cd9-b994-02e2abbb40a5]
34+
description = "count a list with items"
35+
36+
[8272cef5-130d-40ea-b7f6-5ffd0790d650]
37+
description = "count is correct after mutation"
38+
39+
[229b8f7a-bd8a-4798-b64f-0dc0bb356d95]
40+
description = "popping to empty doesn't break the list"
41+
42+
[4e1948b4-514e-424b-a3cf-a1ebbfa2d1ad]
43+
description = "shifting to empty doesn't break the list"
44+
45+
[e8f7c600-d597-4f79-949d-8ad8bae895a6]
46+
description = "deletes the only element"
47+
48+
[fd65e422-51f3-45c0-9fd0-c33da638f89b]
49+
description = "deletes the element with the specified value from the list"
50+
51+
[59db191a-b17f-4ab7-9c5c-60711ec1d013]
52+
description = "deletes the element with the specified value from the list, re-assigns tail"
53+
54+
[58242222-5d39-415b-951d-8128247f8993]
55+
description = "deletes the element with the specified value from the list, re-assigns head"
56+
57+
[ee3729ee-3405-4bd2-9bad-de0d4aa5d647]
58+
description = "deletes the first of two elements"
59+
60+
[47e3b3b4-b82c-4c23-8c1a-ceb9b17cb9fb]
61+
description = "deletes the second of two elements"
62+
63+
[7b420958-f285-4922-b8f9-10d9dcab5179]
64+
description = "delete does not modify the list if the element is not found"
65+
66+
[7e04828f-6082-44e3-a059-201c63252a76]
67+
description = "deletes only the first occurrence"

0 commit comments

Comments
 (0)