PHP linked list node swapping
Posted: Wed Jul 23, 2008 10:44 am
I need some help getting two nodes in a linked list swapped. Here's the code:
class Node {
private $prev;
private $next;
private $value;
private $row;
function Node($value = null, $row = null){
$this->value = &$value;
$this->row = &$row;
}
function setPrev($node){
$this->prev = &$node;
}
function setNext($node){
$this->next = &$node;
}
function &getPrev(){
return $this->prev;
}
function &getNext(){
return $this->next;
}
...
}
class NodeList {
private $first;//head
private $last;//tail
private $size;
function NodeList(){
$this->first = new Node();
$this->last = new Node();
$this->size = 0;
}
function addNode($value,$row){
//this works fine
...
}
function swap($nodeA, $nodeB){
//switch the next and prev references between the parameters
???
}
}
class Node {
private $prev;
private $next;
private $value;
private $row;
function Node($value = null, $row = null){
$this->value = &$value;
$this->row = &$row;
}
function setPrev($node){
$this->prev = &$node;
}
function setNext($node){
$this->next = &$node;
}
function &getPrev(){
return $this->prev;
}
function &getNext(){
return $this->next;
}
...
}
class NodeList {
private $first;//head
private $last;//tail
private $size;
function NodeList(){
$this->first = new Node();
$this->last = new Node();
$this->size = 0;
}
function addNode($value,$row){
//this works fine
...
}
function swap($nodeA, $nodeB){
//switch the next and prev references between the parameters
???
}
}