On single linked list of PHP data structure

Source: Internet
Author: User

What is a linked list? (According to Baidu entry query)

A linked list is a non-sequential, non-sequential storage structure on a physical storage unit, and the logical order of the data elements is achieved through the order of the pointers in the linked list. A linked list consists of a series of nodes (each element in the list is called a node) that can be dynamically generated at execution time. Each node contains two parts: one is the data field that stores the data element, and the other is a pointer field that stores the next node address.

After querying the data and observing the site of the different version number of the linked list. The younger brother himself tried to write a PHP version number of a single-linked list, I hope that you understand the link list is helpful.

<?php
Defining nodes
Class node{
public $id;//Node ID
public $data;//Node data
public $next;//point to next node
Public function __construct ($id, $data) {
$this->id= $id;
$this->data= $data;
$this->next=null;
}
}
Class linklist{
private $head;//define head node
Public function __construct ($id =0, $data =null) {
$this->head=new Node ($id, $data);
}
Inserting nodes
Public Function AddNode ($node) {
$head = $this->head;
Check ID conflicts before inserting
if ($this->getnode ($node->id)!=false) {
echo "The ID you want to insert is". $node->id. " The data is ". $node->data." Node already exists for this ID. Please change ID after retry <br> ";
return false;
}
while ($head->next!=null) {
if ($head->next->id >= $node->id) {
Break
}
$head = $head->next;
}
$node->next= $head->next;
$head->next= $node;

}

Delete a node.
Public Function Delnode ($id) {
$head = $this->head;
$temp =false;
Attention. The delete operation needs to find the previous node that needs to be deleted
while ($head->next! = NULL) {
if ($head->next->id== $id) {
$temp =true;
Break
}
$head = $head->next;
}
if ($temp ==true) {
if ($head->next->next==null) {
$head->next=null;
}
Else
$head->next= $head->next->next;

return true;
}
else {
Echo ' did not find ID for '. $id. ' Node <br> ';
}
}

Get linked List
Public Function getList () {
$head = $this->head;
if ($head->next==null) {
Echo ' The list is empty ';
}
while ($head->next!=null) {
if ($head->id! = 0)
echo ' ID is '. $head->id. ' The data in the node is '. $head->data. ' <br/> ';
$head = $head->next;
}
echo ' ID is '. $head->id. ' The data in the node is '. $head->data;
}

Get the node.
Public Function GetNode ($id) {
$head = $this->head;
while ($head!=null) {
if ($head->id== $id)
return $head->data;
$head = $head->next;
}
return false;
}

Get length
Public Function GetLength () {
$head = $this->head;
$num = 0;
while ($head->next!=null) {
$num + +;
$head = $head->next;
}
return $num;
}
}
$a =new linklist;
$a->addnode (new Node (1, "Hello Word"));
$a->addnode (new Node (2,3));
$a->addnode (new Node (2,11123));
$a->addnode (new Node (3,11123));
$a->getlist ();
$b = $a->getnode (3);
$c = $a->getlength ();
Var_dump ($b);
Var_dump ($c);
?>

At the end of the test code, the following is the test result:

younger brother Beginner. Where is not the right hope that everyone can tolerate. Give some advice.


On single linked list of PHP data structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.