PHP implementation single-linked list

Source: Internet
Author: User


<?php

Header ("Content-type:text/html;charset=utf-8");
Linked list nodes
Class Node {
public static $count =-1; Node ID
Public $name; Node name
Public $next; Next node
public $id;

Public function __construct ($name) {
$this->id = self:: $count;
$this->name = $name;
$this->next = null;
Self:: $count + = 1;
}
}
Single linked list
Class Singellinklist {
Private $header;
Private $current;
Public $count;

Construction method
Public function __construct ($data = null) {
$this->header = new node ($data);
$this->current = $this->header;
$this->count = 0;
}

adding node data
Public Function Addlink ($node) {
if ($this->current->next! = null)
$this->current = $this->current->next;
$this->count++;
$node->next = $this->current->next;
$this->current->next = $node;
}

Delete a linked list node
Public Function Dellink ($id) {
$current = $this->header;
$flag = false;
while ($current->next! = null) {
if ($current->next->id = = $id) {
$flag = true;
Break
}
$current = $current->next;
}
if ($flag) {
$this->count--;
$current->next = $current->next->next;
} else {
echo "id= not Found". $id. "The node! <br> ";
}
}

Get linked List
Public Function getlinklist () {
$this->checknull ();
$current = $this->header;
while ($current->next! = null) {
echo ' ID: '. $current->next->id. ' Name: '. $current->next->name. "<br>";
if ($current->next->next = = null) {
Break
}
$current = $current->next;
}
}

Get length
Public Function Getlinklength ()
{
Echo $this->count;
}

Get current node
Public Function GetCurrent ()
{
$this->checknull ();
Echo ' current Node ID: '. $this->current->next->id. ' Name: '. $this->current->next->name. "<br>";
}

Determines whether the empty
Public Function Checknull ()
{
if ($this->header->next = = null) {
echo "Linked list is empty!";
Exit
}
}

Get node Name
Public Function Getlinkbyid ($id) {
$this->checknull ();
$current = $this->header;
while ($current->next! = null) {
if ($current->id = = $id) {
Break
}
$current = $current->next;
}
echo ' Modified ID: '. $current->id. ' Name: '. $current->name. "<br>";
}

Update node Name
Public Function UpdateLink ($id, $name) {
$this->checknull ();
$current = $this->header;
while ($current->next! = null) {
if ($current->id = = $id) {
Break
}
$current = $current->next;
}
return $current->name = $name;
}
}
$list = new Singellinklist ();
$list->addlink (new node (' aaaaaa '));
$list->addlink (new node (' bbbbbb '));
$list->addlink (new node (' CCCCCC '));
$list->addlink (new node (' dddddd '));
echo "All linked list node:<br/>";
$list->getlinklist ();
echo "<br/>";
echo "Current linked list bottom node:<br/>";
$list->getcurrent ();
echo "<br/>";
echo "Modify the:<br/> of the list node ID 0";
$list->updatelink (0, ' 2222222 ');
echo "Find node:<br/> with id 0";
$list->getlinkbyid (0);
echo "<br/>";
echo "Delete linked list Node ID is 0:<br/>";
$list->dellink (0);
echo "All linked list node:<br/>";
$list->getlinklist ();
echo "<br/>";
echo "All linked list node:<br/>";
$list->getlinklength ();
?>

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.