-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.php
More file actions
140 lines (119 loc) · 3.28 KB
/
LinkedList.php
File metadata and controls
140 lines (119 loc) · 3.28 KB
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
<?php declare(strict_types=1);
require_once('LinkedListNode.php');
/**
* Linked list of nodes containing integer values. This linked list will maintain
* an ordered list of nodes from smallest to greatest. Inserting a new node will
* place it in the correct position, so the list should never need to be reordered.
*/
class LinkedList
{
/**
* @var ?LinkedListNode
*/
protected $head = null;
/**
* @var boolean
*/
protected $isReversed = false;
/**
* Return the head node, first node in the list
*
* @return LinkedListNode|null
*/
public function getHead(): ?LinkedListNode
{
return $this->head;
}
/**
* Add a new node in the relevant position in the list
*
* @param integer $value
* @return self
*/
public function add(int $value): self
{
$newNode = new LinkedListNode($value);
if ($this->head === null) {
$this->head = $newNode;
} else {
$previous = $this->_findPrevious($value);
if ($previous === null) {
$newNode->setNext($this->head);
$this->head = $newNode;
} else {
$newNode->setNext($previous->getNext());
$previous->setNext($newNode);
}
}
return $this;
}
/**
* Reverse list
*
* @return self
*/
public function reverse(): self
{
$current = $this->head;
$previous = null;
$next = null;
while ($current !== null) {
$next = $current->getNext();
$current->setNext($previous);
$previous = $current;
$current = $next;
}
$this->head = $previous;
$this->isReversed = !$this->isReversed;
return $this;
}
/**
* Find last node that has a value less-than-or-equal-to the supplied value (if list is forward-ordered).
* If list is reverse ordered, this will find the node with value greater-than-or-equal-to the supplied value.
*
* @param integer $value
* @return LinkedListNode|null
*/
protected function _findPrevious(int $value): ?LinkedListNode
{
$previous = null;
$current = $this->head;
while ($current !== null) {
if ($this->_isNodeAfterValue($current, $value)) {
break;
}
$previous = $current;
$current = $previous->getNext();
}
return $previous;
}
/**
* Check if the supplied node should be placed later in the list than the value passed.
*
* @param LinkedListNode $node
* @param integer $value
* @return boolean
*/
protected function _isNodeAfterValue(LinkedListNode $node, int $value): bool
{
return $this->isReversed
? $value >= $node->getValue()
: $value <= $node->getValue();
}
/**
* Debug list by just echoing the values of each node
*
* @return self
*/
public function debug(): self
{
$node = $this->getHead();
$values = [];
while ($node) {
$values[] = $node->getValue();
$node = $node->getNext();
}
echo implode(', ', $values) . "\n";
return $this;
}
}