-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyLinkedList.php
More file actions
184 lines (159 loc) · 4.14 KB
/
myLinkedList.php
File metadata and controls
184 lines (159 loc) · 4.14 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
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
<?php
// Author: Marguerite Desko
// implement a singly linked list, with some other methods
class myNode {
// variable to hold the data
var $data = null;
// variable to hold the pointer to the next object
var $next = null;
// constructor for myNode class
function __construct($data) {
$this->data = $data;
$this->next = null;
}
} // end class myNode
class myList {
// variable to hold first Node
var $firstNode;
// variable to hold last node
var $lastNode;
// variable to hold the count of items in the list
var $count;
function __construct() {
// set all class variables to their value before the list has any items in
// it
$this->firstNode = null;
$this->lastNode = null;
$this->count = 0;
}
function insertFirst($data) {
// create the node and set its properties
$currentNode = new myNode($data);
$currentNode->next = null;
// then set the list properties for a single item list
$this->firstNode = $currentNode;
$this->lastNode = $currentNode;
$this->count++;
}
function insertLast($data) {
if ($this->firstNode == null) {
$this->insertFirst($data);
}
else {
// create the node and set its properties
$currentNode = new myNode($data);
$currentNode->next = null;
// set the existing last node's "next" value to the current node,
// since we're adding to the end of the list
$this->lastNode->next = $currentNode;
// then make the current node the last node
$this->lastNode = $currentNode;
$this->count++;
}
}
function deleteNode($value) {
$current = $this->firstNode;
$previous = null;
while ($current->data != $value) {
if ($current->next == null) {
return null;
}
$previous = $current;
$current = $previous->next;
}
if ($current->data == $this->firstNode->data) {
if ($current->data == $this->lastNode) {
$this->count--;
return null;
}
$store = $this->firstNode;
$this->firstNode = $store->next;
$this->count--;
}
else {
$previous->next = $current->next;
$this->count--;
}
}
function readList() {
$current = $this->firstNode;
while ($current != null) {
$retstr .= $current->data.' ';
$current = $current->next;
}
return $retstr;
}
function deleteDupeNodes() {
$current = $this->firstNode;
$previous = $this->firstNode;
$checking = $this->firstNode;
$checkingPrevious = $this->firstNode;
while ($current != null) {
$checking = $current->next;
$checkingPrevious = $current;
while ($checking != null) {
if ($checking->data == $current->data) {
$checkingPrevious->next = $checking->next;
$this->count--;
}
$checkingPrevious = $checking;
$checking = $checking->next;
}
$previous = $current;
$current = $current->next;
}
}
// find the element that is k places from the end of the linked list
function findKthFromEnd($k) {
$current = $this->firstNode;
$kth = null;
while ($current != null) {
// when the counter is the same as k, start moving along in sync
// with outer counter, k spaces behind
if ($counter == $k) {
$kth = $this->firstNode;
}
// increment the counter with each go-round
if ($counter > $k) {
$kth = $kth->next;
}
// increment the counter for the next round
$current = $current->next;
$counter++;
}
// return the data at the kth to end node
return $kth->data;
}
// function to move values < value k before any with value >= k
function partitionList($k) {
$current = $this->firstNode;
$start = new myList();
$end = new myList();
while ($current != null) {
if ($current->data < $k) {
$start->insertLast($current->data);
}
else if ($current->data >= $k) {
$end->insertLast($current->data);
}
$current = $current->next;
}
$start->lastNode->next = $end->firstNode;
return $start;
}
}
// TEST
$list = new myList();
for ($i = 0; $i <= 100; $i++) {
$list->insertLast(rand(1,95));
}
echo 'The count is '.$list->count."\n";
//$list->deleteNode('16');
echo $list->readList()."\n";
//$list->deleteDupeNodes();
//`echo $list->readList()."\n";
echo 'The count is '.$list->count."\n";
echo $list->findKthFromEnd('3');
$sortList = $list->partitionList(10);
echo $sortList->readList()."\n";
?>