Created
May 8, 2024 22:14
-
-
Save elishaukpong/1a1570eebd2fb33a1f0a3dd0c0d9f770 to your computer and use it in GitHub Desktop.
LinkedList
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 { | |
private $data; | |
private $next = null; | |
/** | |
* @param $data | |
*/ | |
public function __construct($data) | |
{ | |
$this->data = $data; | |
} | |
public function getData(): string|int | |
{ | |
return $this->data; | |
} | |
public function getNext(): self|null | |
{ | |
return $this->next; | |
} | |
public function setNext(Node $next): void | |
{ | |
$this->next = $next; | |
} | |
} | |
class LinkedList { | |
protected $first; | |
protected $last; | |
protected $count = 0; | |
public function __construct($data) | |
{ | |
$node = new Node($data); | |
$this->first = $node; | |
$this->last = $node; | |
$this->count++; | |
} | |
public function addNode($data) | |
{ | |
$node = new Node($data); | |
$this->last->setNext($node); | |
$this->last = $node; | |
$this->count++; | |
} | |
public function addNodeAt($data, $point): void | |
{ | |
$newNode = new Node($data); | |
if($point > $this->count) { | |
$this->last->setNext($newNode); | |
$this->last = $newNode; | |
return; | |
} | |
if($point == 1) { | |
$newNode->setNext($this->first); | |
$this->first = $newNode; | |
$this->count++; | |
}else { | |
$prev = $this->first; | |
$current = $this->first; | |
$iterationCount = 1; | |
while($current){ | |
if($iterationCount == $point){ | |
$prev->setNext($newNode); | |
$newNode->setNext($current); | |
$this->count++; | |
break; | |
} | |
$prev = $current; | |
$current = $current->getNext(); | |
$iterationCount++; | |
} | |
} | |
} | |
public function deleteNodeAt($point): void | |
{ | |
if ($point == 1) { | |
$this->first = $this->first->getNext(); | |
} else { | |
$prev = $this->first; | |
$current = $this->first; | |
$iterationCount = 1; | |
while ($current) { | |
if ($iterationCount == $point) { | |
$prev->setNext($current->getNext()); | |
break; | |
} | |
$prev = $current; | |
$current = $current->getNext(); | |
$iterationCount++; | |
} | |
} | |
} | |
public function deleteNodeValue($data) | |
{ | |
} | |
public function printList(): void | |
{ | |
$node = $this->first; | |
while($node){ | |
echo $node->getData() . "\n"; | |
$node = $node->getNext(); | |
} | |
} | |
public function toArray(): array | |
{ | |
$node = $this->first; | |
$array = []; | |
while($node){ | |
$array[] = $node; | |
$node = $node->getNext(); | |
} | |
return $array; | |
} | |
} | |
$list = new LinkedList('Elisha'); | |
$list->addNode('Shalom'); | |
$list->addNode('Chioma'); | |
$list->addNode('Joseph'); | |
$list->addNode('Andikan'); | |
$list->addNodeAt('HRW', 2); | |
$list->addNodeAt('Ndie', 5); | |
$list->addNodeAt('Blessing', 1); | |
$list->addNodeAt('Emek', 10); | |
$list->deleteNodeAt(3); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment