您好,欢迎访问一九零五行业门户网

在python的开发过程中如何进行单链表的实现(代码)

本文介绍了如何实现单链表,希望大家耐心学习。          
//节点class node{ //初始化变量,包括存储的内容 和 下一个数据的指针 public $id = 0; public $data = ''; public $next = null; //构造函数,设置存储内容的数据 public function __construct($id, $data) { $this->id = $id; $this->data = $data; }}//单链表 class singellinklist{ private $header; //链表头节点 //添加节点数据 public function addlink($id = null, $name = null) { $node = new node ($id, $name); $current = $this->header; if (!$current) { $this->header = $node; } else { # 链表头插 $node->next = $current; $this->header = $node; # 链表尾插 /*# 循环,获取对象中最后一个元素 while ($current->next != null) { $current = $current->next; } # 最后一个元素的next指针指向$node $current->next = $node;*/ } } public function dellink($id = null, $name = null) { $current = $this->header; # 循环 while ($current->next != null) { # 查找待删除元素 $delcurrent 的上一个元素 if ($current->next->id == $id) { $delcurrent = $current->next; # 查找待删除元素 $delcurrent 的下一个元素 $current->next = $delcurrent->next; # 删除元素 $delcurrent $delcurrent = null; break; } $current = $current->next; } }}$lists = new singellinklist();$lists->addlink(1, 'aaaaaa');$lists->addlink(2, 'bbbbbb');$lists->addlink(3, 'cccccc');$lists->addlink(4, 'dddddd');$lists->addlink(5, 'eeeeee');$lists->dellink(4);echo '<pre>'; print_r($lists);
以上就是在python的开发过程中如何进行单链表的实现(代码)的详细内容。
其它类似信息

推荐信息