Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save matusstafura/c53bf80421d0b312a9b42193f68ac5ea to your computer and use it in GitHub Desktop.
Save matusstafura/c53bf80421d0b312a9b42193f68ac5ea to your computer and use it in GitHub Desktop.
Basic operations in doubly linked list
<?php
class Node
{
public $value;
public $next;
public $prev;
public function __construct($value)
{
$this->value = $value;
$this->prev = null;
$this->next = null;
}
}
class DoublyLinkedList
{
public $head;
public $tail;
public $length;
public function __construct(public int $value)
{
$newNode = new Node($value);
$this->head = $newNode;
$this->tail = $newNode;
$this->length = 1;
}
public function printAll()
{
$temp = $this->head;
while ($temp != null) {
echo $temp->value;
$temp = $temp->next;
}
echo PHP_EOL;
}
public function append($value)
{
$newNode = new Node($value);
if ($this->length == 0) {
$this->head = $newNode;
$this->tail = $newNode;
} else {
$newNode->prev = $this->tail;
$this->tail->next = $newNode;
$this->tail = $newNode;
}
$this->length++;
}
public function getNode($index)
{
if ($index < 0 || $index >= $this->length) {
return null;
}
$temp = $this->head;
for ($i = 0; $i < $index; $i++) {
$temp = $temp->next;
}
return $temp;
}
public function getNodeOptimized($index)
{
if ($index < 0 || $index >= $this->length) {
return null;
}
if ($index < $this->length / 2) {
$temp = $this->head;
for ($i = 0; $i < $index; $i++) {
$temp = $temp->next;
}
} else {
$temp = $this->tail;
for ($i = $this->length - 1; $i > $index; $i--) {
$temp = $temp->prev;
}
}
return $temp;
}
public function setNode($index, $value)
{
$temp = $this->getNode($index);
if ($temp) {
$temp->value = $value;
}
}
public function prepend($value)
{
$newNode = new Node($value);
if ($this->length == 0) {
$this->head = $newNode;
$this->tail = $newNode;
} else {
$newNode->next = $this->head;
$this->head->prev = $newNode;
$this->head = $newNode;
}
$this->length++;
}
public function insert($index, $value)
{
if ($index < 0 || $index > $this->length) {
return null;
}
if ($index == 0) {
$this->prepend($value);
return;
}
if ($index == $this->length) {
$this->append($value);
return;
}
$newNode = new Node($value);
$before = $this->getNode($index - 1);
$after = $before->next;
$newNode->prev = $before;
$newNode->next = $after;
$before->next = $newNode;
$after->prev = $newNode;
$this->length++;
}
public function popFirst()
{
if ($this->length == 0) {
return null;
}
$temp = $this->head;
if ($this->length == 1) {
$this->head = null;
$this->tail = null;
} else {
$this->head = $this->head->next;
$this->head->prev = null;
$temp->next = null;
}
$this->length--;
return $temp;
}
public function popLast()
{
if ($this->length == 0) {
return null;
}
$temp = $this->tail;
if ($this->length == 1) {
$this->head = null;
$this->tail = null;
} else {
$this->tail = $this->tail->prev;
$this->tail->next = null;
$temp->prev = null;
}
$this->length--;
}
public function remove($index)
{
if ($index < 0 || $index >= $this->length) {
return null;
}
if ($index == 0) {
return $this->popFirst();
}
if ($index == $this->length - 1) {
return $this->popLast();
}
$temp = $this->getNode($index);
$temp->next->prev = $temp->prev;
$temp->prev->next = $temp->next;
$temp->next = null;
$temp->prev = null;
$this->length--;
return $temp;
}
}
$dll = new DoublyLinkedList(2);
$dll->append(7);
$dll->append(3);
$dll->append(9);
// $node = $dll->getNode(1);
// echo $node->value;
// $dll->setNode(1,54);
// $dll->prepend(15);
// $dll->insert(2, 77);
// $dll->popFirst();
// $dll->popLast();
// $dll->remove(2);
$dll->printAll();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment