PHP 实现单链表
header("Content-type:text/html;charset=utf-8");
//链表节点
class node {
public static $count = -1; //节点id
public $name; //节点名称
public $next; //下一节点
public $id;
public function __construct($name) {
$this->id = self::$count;
$this->name = $name;
$this->next = null;
self::$count += 1;
}
}
//单链表
class singelLinkList {
private $header;
private $current;
public $count;
//构造方法
public function __construct($data = null) {
$this->header = new node ($data);
$this->current = $this->header;
$this->count = 0;
}
//添加节点数据
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;
}
//删除链表节点
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=" . $id . "的节点!
";
}
}
//获取链表
public function getLinkList() {
$this->checkNull();
$current = $this->header;
while ( $current->next != null ) {
echo 'id:' . $current->next->id . ' name:' . $current->next->name . "
";
if ($current->next->next == null) {
break;
}
$current = $current->next;
}
}
//获取长度
public function getLinkLength()
{
echo $this->count;
}
//获取当前节点
public function getCurrent()
{
$this->checkNull();
echo '当前节点id:' . $this->current->next->id . ' name:' . $this->current->next->name . "
";
}
//判断是否为空
public function checkNull()
{
if ($this->header->next == null) {
echo "链表为空!";
exit;
}
}
//获取节点名字
public function getLinkById($id) {
$this->checkNull();
$current = $this->header;
while ( $current->next != null ) {
if ($current->id == $id) {
break;
}
$current = $current->next;
}
echo '修改后id:' . $current->id . ' name:' . $current->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 "所有链表节点:
";
$list->getLinkList();
echo "
";
echo "当前链表末位节点:
";
$list->getCurrent();
echo "
";
echo "修改链表节点id为0的:
";
$list->updateLink(0, '2222222');
echo "查找id为0的节点:
";
$list->getLinkById(0);
echo "
";
echo "删除链表节点id为0:
";
$list->delLink(0);
echo "所有链表节点:
";
$list->getLinkList();
echo "
";
echo "所有链表节点:
";
$list->getLinkLength ();
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
