爱python的小吴 2022-09-25 00:34 采纳率: 50%
浏览 20
已结题

python单链表尾部添加节点报错,逻辑貌似没问题啊,求解答!

问题遇到的现象和发生背景

python实现单链表尾部添加节点报错

运行结果及报错内容
class Node:
    def __init__(self,data):
        self.data=data
        self.next=None
        return
    def has_value(self,value):
        if self.data==value:
            return True
        else:
            return False
class singlelink:
    def __init__(self):
        self.head=None
        self.tail=None
        self.length=0
        return
    def isempty(self):
        return self.length==0
    def add_node(self,item):
        if not isinstance(item,Node):
            item=Node(item)
        if self.head is None:
            self.head=item
        else:
            self.tail.next=item
            self.tail=item
        self.length+=1
        return
    def insert_node(self,index,data):
        if self.isempty():
            print("this link is empty!")
            return
        if index<0 or index>=self.length:
            print("error:out of index!")
            return
        item=Node(data)
        if index==0:
            item.next=self.head
            self.haed=item
            self.length+=1
            return
        j=0
        node=self.head
        prev=self.head
        while node.next and j<index:
            prev=node
            node=node.next
            j+=1
        if j==index:
            item._next=node
            prev._next=item
            self.length+=1
    def delete_node_byid(self,item_id):
        id=1
        current_node=self.head
        previous_node=None
        while current_node is not None:
            if id==item_id:
                if previous_node is not None:
                    previous_node.next=current_node.next
                else:
                    self.head=current_node.next
                    return
            previous_node=current_node
            current_node=current_node.next
            id=id+1
        self.length-=1
        return
    def find_node(self,value):
        current_node=self.head
        node_id=1
        results=[]
        while current_node is not None:
            if current_node.has_value(value):
                results.append(node_id)
            current_node=current_node.next
            node_id=node_id+1
        return
    def print_link(self):
        current_node=self.head
        while current_node is not None:
            print(current_node.data)
            current_node=current_node.next
        return
Node1=Node(1)
Node2=Node(2)
Node3=Node(3)
link=singlelink()
for node in [Node1,Node2,Node3]:
    link.add_node(node)
link.print_link()

runfile('C:/Users/Administrator/.spyder-py3/temp.py', wdir='C:/Users/Administrator/.spyder-py3')
Traceback (most recent call last):

  File ~\.spyder-py3\temp.py:90 in <module>
    link.add_node(node)

  File ~\.spyder-py3\temp.py:25 in add_node
    self.tail.next=item

AttributeError: 'NoneType' object has no attribute 'next'
  • 写回答

2条回答 默认 最新

  • 梦里逆天 2022-09-25 08:24
    关注

    img

    class Node:
        def __init__(self, data):
            self.data = data
            self.next = None
            return
    
        def has_value(self, value):
            if self.data == value:
                return True
            else:
                return False
    
    
    class singlelink:
        def __init__(self):
            self.head = None
            self.tail = None
            self.length = 0
            return
    
        def isempty(self):
            return self.length == 0
    
        def add_node(self, item):
            # print(isinstance(item, Node))
            # print('data', item.data)
            # print('next', item.next)
            # print(type(self.head))
            # print(type(self.tail))
            # print(self.tail)
            # print(self.head is None)
            # print(self.tail is None)
            if not isinstance(item, Node):
                item = Node(item)
            if self.head is None:
                self.head = item
                self.tail = item
            else:
                self.tail.next = item
                self.tail = item
            self.length += 1
            return
    
        def insert_node(self, index, data):
            if self.isempty():
                print("this link is empty!")
                return
            if index < 0 or index >= self.length:
                print("error:out of index!")
                return
            item = Node(data)
            if index == 0:
                item.next = self.head
                self.head = item
                self.length += 1
                return
            j = 0
            node = self.head
            prev = self.head
            while node.next and j < index:
                prev = node
                node = node.next
                j += 1
            if j == index:
                item._next = node
                prev._next = item
                self.length += 1
    
        def delete_node_byid(self, item_id):
            id = 1
            current_node = self.head
            previous_node = None
            while current_node is not None:
                if id == item_id:
                    if previous_node is not None:
                        previous_node.next = current_node.next
                    else:
                        self.head = current_node.next
                        return
                previous_node = current_node
                current_node = current_node.next
                id = id + 1
            self.length -= 1
            return
    
        def find_node(self, value):
            current_node = self.head
            node_id = 1
            results = []
            while current_node is not None:
                if current_node.has_value(value):
                    results.append(node_id)
                current_node = current_node.next
                node_id = node_id + 1
            return
    
        def print_link(self):
            current_node = self.head
            while current_node is not None:
                print(current_node.data)
                current_node = current_node.next
    
    
    Node1 = Node(1)
    Node2 = Node(2)
    Node3 = Node(3)
    link = singlelink()
    for node in [Node1, Node2, Node3]:
        # print(node.data)
        link.add_node(node)
    link.print_link()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月3日
  • 已采纳回答 9月25日
  • 创建了问题 9月25日

悬赏问题

  • ¥15 有没有办法利用libusb读取usb设备数据
  • ¥15 为什么openeluer里面按不了python3呢?
  • ¥15 关于#matlab#的问题:训练序列与输入层维度不一样
  • ¥15 关于Ubuntu20.04.3LTS遇到的问题:在安装完CUDA驱动后,电脑会进入卡死的情况,但可以通过键盘按键进入安全重启,但重启完又会进入该情况!
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥15 C++数组中找第二小的数字程序纠错
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)