$pointer->next->next = $pointer->next->next->next; } else { $pointer->next->next = null; } } public function insert_after($value, $node_value) { $pointer = $this->head; $newNode = new Node($value); while ($pointer->value != $node_value) { $pointer = $pointer->next; } $newNode->prev = $pointer; $newNode->next = $pointer->next; $pointer->next->prev = $newNode; $pointer->next = $newNode; } public function displayAll() { var_dump($this->head); var_dump($this->head->next); return $this; } } $list = new DoublyLinkedList(); $list->add_node(1); $list->add_node(2); $list->add_node(3); $list->delete_node(2); $list->insert_after(2, 1); var_dump($list);
$node = $this->head; //have $node point to where $this->head is pointing to if ($node == NULL) { //if $node points to NULL echo "Linked List is empty"; return NULL; } $current = $current->next; while ($node->next != NULL) { echo "Node value is " . $node->value . "<br />"; $node = $node->next; //now have $node point to where $node->next is pointing to } echo "Node value is " . $node->value . "<br />"; //have it print the last node value } } $dll = new DoublyLinkedList(); //creates a new instance of the doubly linked list $dll->add(5); //should add a new node with the value of 5 $dll->add(7); //should add a new node with the value of 7 $dll->add(10); var_dump($dll); // $dll->removeAllNodesWithValueOf(7); //should remove all nodes with value of 7 // $dll->remove(1); //removes node of position 1 // $dll->add(3); //should add a new node with the value of 3 // $dll->insert(1, 100); //insert a new node of value 100 in the position 1 $dll->printValues(); //should print all the values in the doubly linked list
$this->{$head}->next->prev = $node; } else { if ($node->next == null) { $node->prev->next = null; } else { $node->prev->next = $node->next; $node->next->prev = $node->prev; } } } public function displayAll() { var_dump($this->head); var_dump($this->head->next); return $this; } } $list = new DoublyLinkedList(); $list->insert_Beginning(1); $list->insert_Beginning(2); $list->insert_Beginning(3); $list->insert_Before(4, 2); //$list->insert_End($list->head,3); var_dump($list); //$list->insert_After(2,1); //$list->insert_After(3,2); //$list->insert_End() // $list->add_node(2); //$list->add_node(3); //$list->add_node(4); //$list->displayAll();
public function testPushFrontList() { $l = new DoublyLinkedList(); $e1 = $l->pushFront(1); $e2 = $l->pushBack(2); $e3 = $l->pushBack(3); $other = new DoublyLinkedList(); $e4 = $other->pushBack(4); $e5 = $other->pushBack(5); $l->pushFrontList($other); $this->assertEquals(5, $l->len()); $this->assertEquals(4, $l->getFront()->getValue()); $this->assertEquals(5, $l->getFront()->getNext()->getValue()); $this->assertEquals($e1, $l->getFront()->getNext()->getnext()); // pushFrontList the same list will double its elements. $other->pushFrontList($other); $this->assertEquals(4, $other->len()); $this->assertEquals(4, $other->getFront()->getValue()); $this->assertEquals(5, $other->getFront()->getNext()->getValue()); $this->assertEquals(4, $other->getFront()->getNext()->getNext()->getValue()); // pushFrontList empty list won't alter the list. $emptyList = new DoublyLinkedList(); $other->pushFrontList($emptyList); $this->assertEquals(4, $other->len()); }
//have $node point to where $this->head is pointing to if ($node == NULL) { //if $node points to NULL echo "Linked List is empty"; return NULL; } while ($node->next != NULL) { echo "Node value is " . $node->value . "<br />"; $node = $node->next; //now have $node point to where $node->next is pointing to } echo "Node value is " . $node->value . "<br />"; //have it print the last node value } } $dll = new DoublyLinkedList(); //creates a new instance of the doubly linked list $dll->add(5); //should add a new node with the value of 5 $dll->add(7); //should add a new node with the value of 7 $dll->add(10); $dll->removeAllNodesWithValueOf(7); //should remove all nodes with value of 7 $dll->remove(1); //removes node of position 1 $dll->add(3); //should add a new node with the value of 3 $dll->insert(1, 100); //insert a new node of value 100 in the position 1 $dll->printValues();
$current->prev->next = $current->next; $current->next->prev = $current->prev; $current->prev = NULL; $current->next = NULL; $current->value = NULL; } } return $this; } function insertNode($value, $insertion_point) { $current = $this->head; while ($current->value !== $insertion_point) { $current = $current->next; } if ($current === $this->tail) { $this->addNode($value); } else { $insert = new Node($value); $insert->next = $current->next; $current->next = $insert; $insert->prev = $current; $insert->next->prev = $insert; return $this; } } } $list = new DoublyLinkedList(1); $list->addNode(2)->addNode(3)->insertNode(4, 3); $list->deleteNode(6); var_dump($list);
/** * Inserts a copy of an other list at the front of list. * * Other list and current list may be the same. * * @param DoublyLinkedList $other Other list to insert * * @api */ public function pushFrontList(DoublyLinkedList $other) { for ($i = $other->len(), $el = $other->getBack(); $i > 0; $i--, $el = $el->getPrev()) { $this->insertValue($el->getValue(), $this->root); } }