• 数据结构与算法-(11)---有序表(OrderedList)


    🌈个人主页: Aileen_0v0
    🔥系列专栏:PYTHON学习系列专栏
    💫"没有罗马,那就自己创造罗马~" 

    目录

    知识回顾及总结 

    有序表的引入

    ​编辑 实现有序表

    1.有序表-类的构造方法

    2.有序表-search方法的实现 

    3.有序表-add方法的实现

    有序链表 - 完整实现过程

    链表分析


    知识回顾及总结 

    上一次我们学习了无序表之链表和列表,知道了链表的特点是顺藤摸瓜结构

    通俗的讲就是链表相当于火车(如果元素放在链表后面,找那个车厢需要从头开始往后找)

    有序表的引入

    今天,我们来学习有序表- OrderedList-需要增加属性进行位置参照-所以需要对表头进行处理

    有序表是一种数据项依照其某可比性质如整数大小、字母表先后)来决定在列表中的位置
    数值越小位置越前,数值越大位置越后.

     

     实现有序表

    1.有序表-类的构造方法

    1. class Orderedlist:
    2. def __init__(self):
    3. self.head = None

    2.有序表-search方法的实现 

     之前,我们无序表搜索,需要遍历节点,直到找到目标节点,或者没有节点可以继续访问.

    但是,对于有序表,如果目标元素不在列表中,可以利用元素有序的特点终止寻找.

    只要节点中的值比正在查找的值更大,搜索会立刻结束并返回False,因为查找的元素不可能存在于链表后续的节点中.

    1. def search(self,item):
    2. current = self.head
    3. found = False
    4. stop = False
    5. while current != None and not found and not stop:
    6. if current.get_data() == item:
    7. found = True
    8. else:
    9. if current.get_data() > item:
    10. stop = True
    11. else:
    12. current = current.get_next()
    13. return found

    3.有序表-add方法的实现

    相对于无序列表来说,有序列表,需要修改最多的是add方法.

    对于无序表:add方法将一个节点放在最容易访问的位置,即列表头部.

    对于有序列表:需要在需要在已有链表中,为新节点找到正确的插入位置.

    当访问完所有节点(current是None) 或者 当前值大于要添加的元素时,就找到了插入位置,如上图中,找到54即可停止查找.

    有序表和无序表一样,由于current本身无法提供对待修改节点进行访问,

    因此我们需要额外引用previous

    1. def add(self,item):
    2. #初始化两个外部引用(作用相当于指针)
    3. current = self.head#指针1
    4. previous = None#p2
    5. stop = False
    6. #判断循环是否继续执行,---循环停止,就是找到了新节点的插入位置
    7. while current != None and not stop:
    8. #发现插入位置
    9. if current.get_data() > item:
    10. stop = True
    11. else:
    12. previous = current
    13. current = current.get_next()
    14. temp = Node(item)
    15. #插在表头
    16. if previous == None:
    17. temp.set_next(self.head)
    18. self.head = temp
    19. #插在表中
    20. else:
    21. temp.set_next(current)
    22. previous.set_next(temp)

    有序链表 - 完整实现过程

    其它实现过程类似于无序表,可以自己尝试练习一下~

    这里是我的实现过程,仅供大家学习参考.

    1. class Node:#结点Node相当于车厢
    2. def __init__(self,init_data):
    3. self.data = init_data
    4. self.next = None
    5. #获得数据项
    6. def get_data(self):
    7. return self.data
    8. #获得节点
    9. def get_next(self):
    10. return self.next
    11. #设置数据项
    12. def set_data(self,new_data):
    13. self.data = new_data#属性
    14. #设置节点
    15. def set_next(self,new_next):
    16. self.next = new_next#属性
    17. class Orderedlist:
    18. def __init__(self):
    19. self.head = None
    20. def search(self,item):
    21. current = self.head
    22. found = False
    23. stop = False
    24. while current != None and not found and not stop:
    25. if current.get_data() == item:
    26. found = True
    27. else:
    28. if current.get_data() > item:
    29. stop = True
    30. else:
    31. current = current.get_next()
    32. return found
    33. def add(self,item):
    34. current = self.head#指针1
    35. previous = None#p2
    36. stop = False
    37. while current != None and not stop:
    38. #发现插入位置
    39. if current.get_data() > item:
    40. stop = True
    41. else:
    42. previous = current
    43. current = current.get_next()
    44. temp = Node(item)
    45. #插在表头
    46. if previous == None:
    47. temp.set_next(self.head)
    48. self.head = temp
    49. #插在表中
    50. else:
    51. temp.set_next(current)
    52. previous.set_next(temp)
    53. def size(self):
    54. current = self.head
    55. count = 0
    56. while current != None:
    57. count += 1
    58. current = current.get_next()
    59. return count
    60. def remove(self, item):
    61. current = self.head
    62. previous = None
    63. found = False
    64. while not found and current != None:
    65. if current.get_data() == item:
    66. found = True
    67. else:
    68. previous = current
    69. current = current.get_next()
    70. if found:
    71. if previous == None:
    72. self.head = current.get_next()
    73. else:
    74. previous.set_next(current.get_next())
    75. def traverse(self):
    76. current = self.head
    77. while current != None:
    78. print(current.get_data())
    79. current = current.get_next()
    80. ol = Orderedlist()
    81. ol.add(7)
    82. ol.add(9)
    83. ol.add(6)
    84. ol.add(8)
    85. ol.add(10)
    86. print(ol.search(6))
    87. ol.traverse()

    链表分析

  • 相关阅读:
    DBeaver安装与使用教程(超详细安装与使用教程),好用免费的数据库管理工具
    【性能测试】Loadrunner12.55(二)-飞机订票系统-脚本录制
    nvm管理多个版本的nodejs
    Electron程序如何在MacOS下获取相册访问权限
    Hive基础进阶10大技巧
    【C++】初窥C++
    Cobaltstrike —— shellcode分析(一)
    AI学习教程:AI(Adobe lliustrator)快速入门
    CDN的基本概念
    防抖及其优化
  • 原文地址:https://blog.csdn.net/Aileenvov/article/details/134279860