资讯专栏INFORMATION COLUMN

【php实现数据结构】单向链表

piapia / 2044人阅读

摘要:什么是单向链表链表是以链式存储数据的结构,其不需要连续的存储空间,链表中的数据以节点来表示,每个节点由元素存储数据和指针指向后继节点组成。单向链表也叫单链表是链表中最简单的一种形式,每个节点只包含一个元素和一个指针。

什么是单向链表

链表是以链式存储数据的结构,其不需要连续的存储空间,链表中的数据以节点来表示,每个节点由元素(存储数据)和指针(指向后继节点)组成。

单向链表(也叫单链表)是链表中最简单的一种形式,每个节点只包含一个元素和一个指针。
它有一个表头,并且除了最后一个节点外,所有节点都有其后继节点。
它的存储结构如下图所示

代码实现

定义节点

</>复制代码

  1. class Node
  2. {
  3. public $data;
  4. /**
  5. * @var null | Node
  6. */
  7. public $next;
  8. public function __construct($data)
  9. {
  10. $this->data = $data;
  11. $this->next = null;
  12. }
  13. }

单链表实现

</>复制代码

  1. /**
  2. * Class SingleLinkList
  3. * 单链接的实现示例,实现简单的填加,插入,删除, 查询,长度,遍历这几个简单操作
  4. */
  5. class SingleLinkList
  6. {
  7. /**
  8. * 链表头结点,头节点必须存在,
  9. * @var Node
  10. */
  11. public $header;
  12. private $size = 0;
  13. /**
  14. * 构造函数,默认填加一个哨兵节点,该节点元素为空
  15. * SingleLinkList constructor.
  16. */
  17. public function __construct()
  18. {
  19. $this->header = new Node(null);
  20. }
  21. /**
  22. * 在链表末尾添加节点
  23. * @param Node $node
  24. * @return int
  25. */
  26. public function addNode(Node $node)
  27. {
  28. $current = $this->header;
  29. while ($current->next != null) {
  30. $current = $current->next;
  31. }
  32. $current->next = $node;
  33. return ++$this->size;
  34. }
  35. /**
  36. * 在指定位置插入节点
  37. * @param int $index 节点位置,从1开始计数
  38. * @param Node $node
  39. * @return int
  40. * @throws Exception
  41. */
  42. public function insertNodeByIndex($index, Node $node)
  43. {
  44. if ($index < 1 || $index > ($this->size + 1)) {
  45. throw new Exception(sprintf("你要插入的位置,超过了链表的长度 %d", $this->size));
  46. }
  47. $current = $this->header;
  48. $tempIndex = 1;
  49. do {
  50. if ($index == $tempIndex++) {
  51. $node->next = $current->next;
  52. $current->next = $node;
  53. break;
  54. }
  55. } while ($current->next != null && ($current = $current->next));
  56. return ++$this->size;
  57. }
  58. /**
  59. * 删除节点
  60. * @param int $index 节点位置,从1开始计数
  61. * @return int
  62. * @throws Exception
  63. */
  64. public function deleteNodeByIndex($index)
  65. {
  66. if ($index < 1 || $index > ($this->size + 1)) {
  67. throw new Exception("你删除的节点不存在");
  68. }
  69. $current = $this->header;
  70. $tempIndex = 1;
  71. do {
  72. if ($index == $tempIndex++) {
  73. $current->next = $current->next->next;
  74. break;
  75. }
  76. } while ($current->next != null && ($current = $current->next));
  77. return --$this->size;
  78. }
  79. /**
  80. * 查询节点
  81. * @param int $index 节点位置,从1开始计数
  82. * @return Node|null
  83. * @throws Exception
  84. */
  85. public function searchNodeByIndex($index) {
  86. if ($index < 1 || $index > ($this->size + 1)) {
  87. throw new Exception("你查询的节点不存在");
  88. }
  89. $current = $this->header;
  90. $tempIndex = 1;
  91. do {
  92. if ($index == $tempIndex++) {
  93. return $current->next;
  94. }
  95. } while ($current->next != null && ($current = $current->next));
  96. }
  97. /**
  98. * 获取节点长度
  99. * @return int
  100. */
  101. public function getLength()
  102. {
  103. return $this->size;
  104. }
  105. /**
  106. * 遍历列表
  107. */
  108. public function showNode()
  109. {
  110. $current = $this->header;
  111. $index = 1;
  112. while ($current->next != null) {
  113. $current = $current->next;
  114. echo "index --- " . $index++ . " --- ";
  115. echo var_export($current->data);
  116. echo PHP_EOL;
  117. }
  118. }
  119. }

示例

</>复制代码

  1. $link = new SingleLinkList();
  2. $link->addNode(new Node(1));
  3. $link->addNode(new Node(2));
  4. $link->insertNodeByIndex(3, new Node(3));
  5. $link->addNode(new Node(4));
  6. $link->addNode(new Node(5));
  7. echo $link->getLength(), PHP_EOL;
  8. $link->showNode();
  9. echo "-----------", PHP_EOL;
  10. var_dump($link->searchNodeByIndex(3));
  11. echo "-----------", PHP_EOL;
  12. $link->deleteNodeByIndex(3);
  13. $link->showNode();

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/29873.html

相关文章

  • PHP面试常考之数据结构——链表的概念

    摘要:一链表链表是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针。指向整个列表的指针可以被称作访问指针。 你好,是我琉忆,PHP程序员面试笔试系列图书的作者。 本周(2019.3.18至3.22)的一三五更新的文章如下: 周一:PHP面试常考之数据结构——链表的概念周三:PHP面试常考之数据结构——栈和队列周五:PHP面试常考之...

    dreamans 评论0 收藏0
  • 图解几种常见的线性表

    摘要:下面来看一下,有哪些数据结构属于线性表吧栈特性先进后出只有唯一的一个出入口介绍栈又名堆栈,它是一种运算受限的线性表。 原文是在我自己博客中,小伙伴也可以点阅读原文进行跳转查看,还有好听的背景音乐噢背景音乐已取消~ 2333333 线性表 什么是线性表?就是一种连续或间断存储的数组,这里的连续和间断是针对物理内存空间中线性表元素之间是否连续,其中连续数组对应内置数组的实现方式,间断数组对...

    wawor4827 评论0 收藏0
  • 学习JavaScript数据结构与算法(二):链表

    摘要:实现移除给定的元素要移除的元素返回值表示移除成功方法说明移除单向链表中某个位置的元素。的前端乐园原文链接寒假前端学习学习数据结构与算法二链表 本系列的第一篇文章: 学习JavaScript数据结构与算法(一),栈与队列第二篇文章:学习JavaScript数据结构与算法(二):链表第三篇文章:学习JavaScript数据结构与算法(三):集合第四篇文章:学习JavaScript数据结构与...

    lolomaco 评论0 收藏0
  • JavaScript的数据结构与算法(三) —— 单向链表

    摘要:链表链表存储有序的元素集合,但不同于数组,链表中的元素在内存中并不是连续放置的。相对于传统的数组,链表的一个好处在于,添加或者删除元素的时候不需要移动其他元素。 链表 链表存储有序的元素集合,但不同于数组,链表中的元素在内存中并不是连续放置的。每个元素由一个存储元素本事的节点和一个指向下一个元素的引用组成。相对于传统的数组,链表的一个好处在于,添加或者删除元素的时候不需要移动其他元素。...

    李涛 评论0 收藏0

发表评论

0条评论

piapia

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<