-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinkedList.php
More file actions
153 lines (135 loc) · 3.17 KB
/
linkedList.php
File metadata and controls
153 lines (135 loc) · 3.17 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
<?php
/**
* Very Very Simple Singly Linked List Class.
* @author Steve King <steve@sming.com.au>
*
*/
class linkedList {
// Who's on first
private $_first;
// I don't know is on third.
private $_last;
// Total Count
private $_total;
// Position
private $_pos;
// What's on second.
private $_posData;
/**
* Simple constructor;
*/
public function __construct() {
$this->_first = NULL;
$this->_last = NULL;
$this->_pos = 0;
$this->_total = 0;
}
/**
* Insert (at end) element to the list;
* @param mixed $Input
*/
public function insert($Input = FALSE) {
$Link = new linkedListNode($Input);
if ( $this->_last == NULL && $this->_first == NULL ) {
$Link->next = NULL;
$this->_first = &$Link;
$this->_last = &$Link;
$this->_total++;
}
else {
$Link->next = NULL;
$this->_last->next = $Link;
$this->_last = &$Link;
$this->_total++;
}
}
/**
* Based on the current position, return me the next element;
* @return object linkedListNode
*/
public function next() {
if ( $this->_posData == NULL ) {
$this->_posData = $this->_first;
}
else {
$this->_posData = $this->_posData->next;
}
$this->_pos++;
return $this->_posData->data;
}
/**
* Reverse the list, also reset the position of the active element,
* later it's probably a good idea to think about setting this to
* the right position?
*/
public function reverse() {
// Because we have linked both first and last, we uset the last;
unset($this->_last);
// Now we begin to iterate through from the start;
$linkNode = $this->_first;
// Set the next element to null;
$linkReverse = NULL;
// While nodes are flowing...
while ( $linkNode != NULL ) {
// Set a switcher to the next element;
$Switch = $linkNode->next;
// Set the next element to the last;
$linkNode->next = $linkReverse;
// Set the last element to this one;
$linkReverse = $linkNode;
// And set the first one to the next;
$linkNode = $Switch;
}
// Now find the last element again;
// There's probably a more efficient way
$Switcher = $this->_first;
while ( $Switcher->next != NULL ) {
$Switcher = $Switcher->next;
}
// Now set the last element correctly;
$this->_last = $Switcher;
// Set the first element again;
$this->_first = $linkReverse;
}
/**
* Reset the pointer.
*/
public function reset() {
// Set our pointers back to the start;
$this->_posData = NULL;
$this->_pos = 0;
}
/**
* Return the last element;
*/
public function last() {
// Return the last element
return $this->_last;
}
/**
* Return the first element;
*/
public function first() {
// Return the first element (whole list);
return $this->_first;
}
}
/**
* Simple linked list node class;
* @author Steve King <steve@sming.com.au>
*
*/
class linkedListNode {
public $data;
public $next;
public function __construct($Input = FALSE) {
$this->data = $Input;
$this->next = NULL;
}
/**
* Return the data in the list;
*/
public function read() {
return $this->data;
}
}