Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a linked list implementation in PHP pulled from: <a href="http://www.codediesel.com/php/linked-list-in-php/">http://www.codediesel.com/php/linked-list-in-php/</a> which can add, delete, reverse and empty a linkedlist in PHP.</p> <pre><code>&lt;?php class ListNode { public $data; public $next; function __construct($data) { $this-&gt;data = $data; $this-&gt;next = NULL; } function readNode() { return $this-&gt;data; } } class LinkList { private $firstNode; private $lastNode; private $count; function __construct() { $this-&gt;firstNode = NULL; $this-&gt;lastNode = NULL; $this-&gt;count = 0; } //insertion at the start of linklist public function insertFirst($data) { $link = new ListNode($data); $link-&gt;next = $this-&gt;firstNode; $this-&gt;firstNode = &amp;$link; /* If this is the first node inserted in the list then set the lastNode pointer to it. */ if($this-&gt;lastNode == NULL) $this-&gt;lastNode = &amp;$link; $this-&gt;count++; } //displaying all nodes of linklist public function readList() { $listData = array(); $current = $this-&gt;firstNode; while($current != NULL) { array_push($listData, $current-&gt;readNode()); $current = $current-&gt;next; } foreach($listData as $v){ echo $v." "; } } //reversing all nodes of linklist public function reverseList() { if($this-&gt;firstNode != NULL) { if($this-&gt;firstNode-&gt;next != NULL) { $current = $this-&gt;firstNode; $new = NULL; while ($current != NULL) { $temp = $current-&gt;next; $current-&gt;next = $new; $new = $current; $current = $temp; } $this-&gt;firstNode = $new; } } } //deleting a node from linklist $key is the value you want to delete public function deleteNode($key) { $current = $this-&gt;firstNode; $previous = $this-&gt;firstNode; while($current-&gt;data != $key) { if($current-&gt;next == NULL) return NULL; else { $previous = $current; $current = $current-&gt;next; } } if($current == $this-&gt;firstNode) { if($this-&gt;count == 1) { $this-&gt;lastNode = $this-&gt;firstNode; } $this-&gt;firstNode = $this-&gt;firstNode-&gt;next; } else { if($this-&gt;lastNode == $current) { $this-&gt;lastNode = $previous; } $previous-&gt;next = $current-&gt;next; } $this-&gt;count--; } //empty linklist public function emptyList() { $this-&gt;firstNode == NULL; } //insertion at index public function insert($NewItem,$key){ if($key == 0){ $this-&gt;insertFirst($NewItem); } else{ $link = new ListNode($NewItem); $current = $this-&gt;firstNode; $previous = $this-&gt;firstNode; for($i=0;$i&lt;$key;$i++) { $previous = $current; $current = $current-&gt;next; } $previous-&gt;next = $link; $link-&gt;next = $current; $this-&gt;count++; } } } $obj = new LinkList(); $obj-&gt;insertFirst($value); $obj-&gt;insert($value,$key); // at any index $obj-&gt;deleteNode($value); $obj-&gt;readList(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload