forked from azdanov/php-interview-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.php
201 lines (177 loc) · 3.84 KB
/
LinkedList.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
declare(strict_types=1);
namespace Exercises\LinkedList;
use Iterator;
/**
* Implements Linked List data structure with help of Node class.
*
* This class has single property head that references first node.
*/
final class LinkedList implements Iterator
{
public function __construct()
{
}
/**
* Return a middle node without using counters or size.
* Only iteration is allowed.
*
* When list has even number of items, return the
* last one from the first half.
*/
public static function midpoint(self $list): ?Node
{
}
/**
* Given a linked list detect if there is a circular reference
* inside one of the nodes.
*
* $list = new LinkedList();
* $a = new Node('a');
* $b = new Node('b');
* $c = new Node('c');
*
* $list->head = $a;
* $a->next = $b;
* $b->next = $c;
* $c->next = $a;
* LinkedList::circular($list) === true
*/
public static function circular(self $list): ?Node
{
}
/**
* Return a node that is located n places from the end.
*
* Do not use size, and assume that n is always less than
* the list length.
*/
public static function fromLast(self $list, int $n): ?Node
{
}
/**
* Create a new Node from data and assign the node to the head property.
*
* @param mixed $data
*/
public function insertFirst($data): void
{
}
/**
* Create a new Node from data and assign the node at the end.
*
* @param mixed $data
*/
public function insertLast($data): void
{
}
/**
* @return int number of nodes in the linked list
*/
public function size(): int
{
}
/**
* @return Node first node in the linked list
*/
public function getFirst(): Node
{
}
/**
* @return Node first node in the linked list
*/
public function getLast(): Node
{
}
/**
* Empty the linked list.
*/
public function clear(): void
{
}
/**
* Remove first node from the linked list.
*/
public function removeFirst(): void
{
}
/**
* Remove last node from the linked list.
*/
public function removeLast(): void
{
}
/**
* @return Node at specified index
*/
public function getAt(int $index): Node
{
}
/**
* @param int $index to remove node at
*/
public function removeAt(int $index): void
{
}
/**
* Insert data at position.
*
* @param mixed $data
*/
public function insertAt($data, int $index): void
{
}
/**
* @param callable $callback to call for each node in linked list
*/
public function forEach(callable $callback): void
{
}
/**
* Return the current element.
*
* @see https://php.net/manual/en/iterator.current.php
*
* @return mixed can return any type
*/
public function current()
{
}
/**
* Move forward to next element.
*
* @see https://php.net/manual/en/iterator.next.php
*/
public function next(): void
{
}
/**
* Return the key of the current element.
*
* @see https://php.net/manual/en/iterator.key.php
*
* @return mixed scalar on success, or null on failure
*/
public function key()
{
}
/**
* Checks if current position is valid.
*
* @see https://php.net/manual/en/iterator.valid.php
*
* @return bool The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure.
*/
public function valid(): bool
{
}
/**
* Rewind the Iterator to the first element.
*
* @see https://php.net/manual/en/iterator.rewind.php
*/
public function rewind(): void
{
}
}