zoukankan      html  css  js  c++  java
  • 链表的crud:Python实现

    单链表的定义以及增删查找等操作

      1 class Node(object):
      2     def __init__(self, elem):
      3         self.elem = elem
      4         self.next = None
      5 
      6 
      7 class SingleLinkList(object):
      8     def __init__(self, node=None):
      9         self.__head = node
     10 
     11     def is_empty(self):
     12         return self.__head == None
     13 
     14     def length(self):
     15         """
     16         链表长度
     17         :return:
     18         """
     19         # 用来移动遍历节点
     20         cur = self.__head
     21         count = 0
     22         while cur:
     23             cur = cur.next
     24             count += 1
     25         return count
     26 
     27     def travel(self):
     28         """
     29         遍历链表长度
     30         :return:
     31         """
     32         cur = self.__head
     33         while cur:
     34             print(cur.elem, end=' ')
     35             # print(cur.elem)
     36             cur = cur.next
     37 
     38     def append(self, item):
     39         """
     40         尾部插入
     41         进来先判断是否为空
     42         为空的话 头指针指向插入节点即可
     43         否则需要遍历到尾结点再插入
     44         :return:
     45         """
     46         node = Node(item)
     47 
     48         if self.is_empty():
     49             self.__head = node
     50         else:
     51             cur = self.__head
     52             while cur.next:
     53                 cur = cur.next
     54             cur.next = node
     55 
     56     def insert(self, pos, item):
     57         """
     58         指定位置添加元素
     59         :param pos: 添加的位置
     60         :param item: 添加的元素
     61         :return:
     62         先对pos进行判断,如果小于0 那么赋值为
     63         如果大于最大长度 那么放在最后面
     64         """
     65         # 实例化要插入的点
     66         node = Node(item)
     67         if pos <= 0:
     68             pos = 0
     69         if pos >= self.length() - 1:
     70             pos = self.length() - 1
     71 
     72         pre = self.__head
     73         count = 0
     74         while count < pos - 1:
     75             count += 1
     76             pre = pre.next
     77 
     78         node.next = pre.next
     79         pre.next = node
     80 
     81     def add(self, item):
     82         """
     83         头插法
     84         :param item:添加的是数字,需要实例化成对象
     85         :return:
     86         """
     87         node = Node(item)
     88         node.next = self.__head
     89         self.__head = node
     90 
     91     def remove(self, item):
     92         pre = self.__head
     93         # 要遍历到需要删除节点的前一个节点
     94         while pre.next:
     95             # 这里面还需要对删除第一个元素做单独处理
     96             if item == pre.elem:
     97                 self.__head = pre.next
     98                 break
     99             else:
    100                 # 处理中间和后面的元素
    101                 if pre.next.elem == item:
    102                     pre.next = pre.next.next
    103                 else:
    104                     pre = pre.next
    105 
    106     def search(self, item):
    107         """
    108         全部遍历一遍
    109         :param item:
    110         :return:
    111         """
    112         cur = self.__head
    113         while cur:
    114             if cur.elem == item:
    115                 return True
    116             else:
    117                 cur = cur.next
    118         return False
    119 
    120 
    121 if __name__ == "__main__":
    122     ll = SingleLinkList()
    123     print(ll.is_empty())
    124     ll.append(100)
    125     ll.append(200)
    126     ll.add(10)
    127     ll.append(300)
    128     ll.append(400)
    129     ll.travel()
    130     print("")
    131     print(ll.length())
    132     ll.insert(2, 1000)
    133     ll.travel()
    134     print("")
    135     ll.remove(400)
    136     ll.travel()
    137     print()
    138     print(ll.search(140))
    139     print(ll.search(1000))
    140 """
    141 True
    142 10 100 200 300 400 
    143 5
    144 10 100 1000 200 300 400 
    145 10 100 1000 200 300 
    146 False
    147 True
    148 """
  • 相关阅读:
    js和jquery 两种写法 鼠标经过图片切换背景效果
    phpStudy如何修改端口及WWW目录
    模仿淘宝上传图片之后在图片中单击按钮?
    资源汇总
    标题类型-整型
    VB6 内存释放
    在Textbox中按回车键后继续获取焦点
    ADO 读写文本文件
    VBA 拷贝文件
    VBA 获取文件夹内的文件列表
  • 原文地址:https://www.cnblogs.com/d9e84208/p/10703430.html
Copyright © 2011-2022 走看看