Created
October 22, 2019 19:54
-
-
Save irfanbaigse/14fcc35595f967ba649f29ba8b3330e0 to your computer and use it in GitHub Desktop.
Singly Linked List
This file contains hidden or 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 | |
use DataStructures\LinkedList; | |
$linkedList = new LinkedList(); | |
$linkedList->inserFirst(10); | |
$linkedList->inserFirst(20); | |
$linkedList->insertLast(40); | |
$linkedList->insertLast(30); |
This file contains hidden or 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 | |
namespace DataStructures; | |
class Node | |
{ | |
public $value = null; | |
public $next = null; | |
public function __construct($data) | |
{ | |
$this->value = $data; | |
} | |
public function readNode() | |
{ | |
return $this->value; | |
} | |
} |
This file contains hidden or 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
{ | |
"firstNode": { | |
"value": 20, | |
"next": { | |
"value": 10, | |
"next": { | |
"value": 40, | |
"next": { | |
"value": 30, | |
"next": null | |
} | |
} | |
} | |
}, | |
"lastNode": { | |
"value": 30, | |
"next": null | |
}, | |
"count": 4 | |
} |
This file contains hidden or 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 | |
namespace DataStructures; | |
use DataStructures\Node; | |
class LinkedList | |
{ | |
/** | |
* make as public to print in json | |
*/ | |
public $firstNode = null; | |
public $lastNode = null; | |
public $count = 0; | |
public function inserFirst($data) | |
{ | |
if ($this->firstNode !== null) { | |
$node = $this->getNewNode($data); | |
$node->next = $this->firstNode; | |
$this->firstNode = &$node; | |
$this->count++; | |
} else { | |
$this->insertFirstNode($data); | |
} | |
} | |
public function insertLast($data) | |
{ | |
if ($this->firstNode !== null) { | |
$node = $this->getNewNode($data); | |
$this->lastNode->next = $node; | |
$this->lastNode = &$node; | |
$this->count++; | |
} else { | |
$this->insertFirstNode($data); | |
} | |
} | |
private function insertFirstNode($data) | |
{ | |
$node = $this->getNewNode($data); | |
$this->firstNode = &$node; | |
$this->lastNode = &$node; | |
$this->count++; | |
} | |
public function getCount() | |
{ | |
return $this->count; | |
} | |
private function getNewNode($data) | |
{ | |
return new Node($data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment