zoukankan      html  css  js  c++  java
  • 创建单链表

    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()

    以上链表集成了增删查

  • 相关阅读:
    Java实现直接插入查找
    CSS hack常用方案(摘选)
    输入一个字符串,去掉重复的字符,并按ASCII值排序-华为机试
    git操作github
    简单记录下idea上配置spring
    mysql国内镜像下载网址
    tomcat启动不成功(点击startup.bat闪退)的解决办法
    Encode and Decode TinyURL
    leetcode 4Sum II
    leetcode two sum
  • 原文地址:https://www.cnblogs.com/blackball9/p/11878772.html
Copyright © 2011-2022 走看看