-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.php
52 lines (47 loc) · 1.21 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
<?php
include_once "node.php";
?>
<?php
class Linkedlist
{
private $tail = null;
public function _add($val, $index)
{
$newNode = new Node($val);
if ($index == 0) {
$newNode->next = $this->tail;
$this->tail = $newNode;
} else {
$i = $this->tail;
for ($count = 0; $count < $index - 1; $count++) {
$i = $i->next;
}
$newNode->next = $i->next;
$i->next = $newNode;
}
}
public function _print()
{
$i = $this->tail;
while ($i != null) {
echo $i->value;
echo '<br>' ;
$i = $i->next;
}
}
public function _remove($index)
{
$i = $this->tail;
$x = $this->tail;
if ($index == 0) {
$this->tail = $i->next;
} else {
for ($count = 0; $count < $index - 1; $count++) {
$i = $i->next;
}
$x = $i->next;
$i->next = $x->next;
}
}
}
?>