• 创建单链表


    class Node():
        def __init__(self,item):
            self.item = item
            self.next = None
    class Link():
        #构建出一个空的链表
        def __init__(self):
            self._head = None #永远指向链表中的头节点
            #想链表的头部插入节点
        def add(self,item):
            node = Node(item)
            node.next = self._head
            self._head = node
            
        def travel(self):   
            cur = self._head
            #链表为空则输出‘链表为空’
            if self._head == None:
                print('链表为空!')
            while cur:
                print(cur.item)
                cur = cur.next
        def isEmpty(self):
            return self._head == None
        def length(self):
            cur = self._head
            count = 0
            while cur:
                count += 1
                cur = cur.next
            return count          
        def search(self,item):
            cur = self._head
            find = False
            while cur:
                if cur.item == item:
                    find = True
                    break
                cur = cur.next
            return find
        
        def append(self,item):
            node = Node(item)
            #链表为空的情况
            if self._head == None:
                self._head = node
                return
            
            cur = self._head #头节点
            pre = None #cur的前一个节点
            while cur:
                pre = cur
                cur = cur.next
            pre.next = node
            
        def insert(self,pos,item):
            node = Node(item)
            
            if pos < 0 or pos > self.length():
                print('重新给pos赋值!!!')
                return
            
            cur = self._head
            pre = None
            
            for i in range(pos):
                pre = cur
                cur = cur.next
            pre.next = node
            node.next = cur
        def remove(self,item):
            cur = self._head
            pre = None
            
            if self._head == None:#链表为空
                print('链表为空,没有可删除的节点!!1')
                return
        
            
            #删除的是第一个节点的情况
            if self._head.item == item:
                self._head = self._head.next
                return
            
            #删除的是非第一个节点的情况
            while cur:
                pre = cur
                cur = cur.next
                if cur.item == item:
                    pre.next = cur.next
                    return
                    
    link = Link()
    # link.add(3)
    # link.add(4)
    # link.add(5)
    # link.add(6)
    
    link.remove(3)
    
    link.travel()

    以上链表集成了增删查

  • 相关阅读:
    Pytorch tensor求和( tensor.sum())
    Pytorch torch.cat(inputs, dimension=0)
    README.md编写
    Numpy-np.random.normal()正态分布
    numpy数组的分割与合并
    使用Pandas读取CSV文件
    train loss与test loss结果分析/loss不下降
    Python-给数字/字符串前加0
    SVM简单上手示例
    迁移学习简介
  • 原文地址:https://www.cnblogs.com/blackball9/p/11878772.html
Copyright © 2020-2023  润新知