Last active
December 27, 2022 14:09
-
-
Save matusstafura/6d7bf89bdde17e83fa39def977ffe7aa to your computer and use it in GitHub Desktop.
Basic operations in Singly Linked List In PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Node | |
{ | |
public $value; | |
public $next; | |
public function __construct($value) | |
{ | |
$this->value = $value; | |
$this->next = null; | |
} | |
} | |
class LinkedList | |
{ | |
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 append($value) | |
{ | |
$newNode = new Node($value); | |
if ($this->length == 0) { | |
$this->head = $newNode; | |
$this->tail = $newNode; | |
} else { | |
$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 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 = $newNode; | |
} | |
$this->length++; | |
} | |
public function insert($index, $value) | |
{ | |
if ($index < 0 || $index >= $this->length) { | |
return null; | |
} | |
if ($index == 0) { | |
$this->prepend($value); | |
return; | |
} elseif ($index == $this->length) { | |
$this->append($value); | |
return; | |
} | |
$newNode = new Node($value); | |
$temp = $this->getNode($index - 1); | |
$newNode->next = $temp->next; | |
$temp->next = $newNode; | |
$this->length++; | |
} | |
public function popFirst() | |
{ | |
if ($this->length == 0) { | |
return null; | |
} | |
if ($this->length == 1) { | |
$this->head = null; | |
$this->tail = null; | |
} else { | |
$temp = $this->head; | |
$this->head = $temp->next; | |
$temp->next = null; | |
} | |
$this->length--; | |
} | |
public function pop() | |
{ | |
if ($this->length == 0) { | |
return null; | |
} | |
if ($this->length == 1) { | |
$this->head = null; | |
$this->tail = null; | |
} else { | |
$temp = $this->head; | |
$prev = $this->head; | |
while ($temp->next) { | |
$prev = $temp; | |
$temp = $temp->next; | |
} | |
$this->tail = $prev; | |
$this->tail->next = null; | |
} | |
$this->length--; | |
} | |
public function remove($index) | |
{ | |
if ($index < 0 || $index >= $this->length) { | |
return null; | |
} | |
if ($index == 0) { | |
return $this->popFirst(); | |
} elseif ($index == $this->length - 1) { | |
return $this->pop(); | |
} | |
$temp = $this->getNode($index - 1); | |
$temp->next = $temp->next->next; | |
$this->length--; | |
} | |
public function printAll() | |
{ | |
$temp = $this->head; | |
while ($temp != null) { | |
echo $temp->value . '->'; | |
$temp = $temp->next; | |
} | |
echo PHP_EOL; | |
} | |
} | |
$ll = new LinkedList(1); | |
$ll->append(22); | |
$ll->append(7); | |
// Get Node | |
// $ll->getNode(0); | |
// Set Node | |
// $ll->setNode(1,33); | |
// Prepend Node | |
// $ll->prepend(13); | |
// Insert Node | |
// $ll->insert(1,13); | |
// Pop First | |
// $ll->popFirst(); | |
// Pop Last | |
// $ll->pop(); | |
$ll->printAll(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment