单链表推送功能

问题描述

以下内容来自 Zed Shaws 的“Learn More Python The Hard Way”。 我在底部方法调用旁边评论了push to the list的输出

import { isPersian,toPersianChars } from "persian-tools2";

URLfix(
    "https://fa.wikipedia.org/wiki/%D9%85%D8%AF%DB%8C%D8%A7%D9%88%DB%8C%DA%A9%DB%8C:Gadget-Extra-Editbuttons-botworks.js",); // "https://fa.wikipedia.org/wiki/مدیاویکی:Gadget-Extra-Editbuttons-botworks.js"
URLfix("https://en.wikipedia.org/wiki/Persian_alphabet"); // "https://en.wikipedia.org/wiki/Persian_alphabet",URLfix("Sample Text"); // "Sample Text"

到目前为止,我确信 class sllNode(object): def __init__(self,data=None,next_node=None): self.data = data self.next_node = next_node def __repr__(self): nval = self.data and self.next_node or None return f"[{self.data}:{repr(nval)}]" class LinkedList(object): def __init__(self,head=None): self.begin = None self.end = None def push(self,data): Node = sllNode(data,None) if self.begin == None: self.end = Node # self.begin = self.end # for life of the program? else: self.end.next_node = Node self.end = Node # why wont self.begin change to just Node like self.end does here...? aren't they linked? test = LinkedList() test.push(1) #self.begin = 1:None,self.end = 1:None test.push(2) #self.begin = 1:2:None,self.end = 2:None test.push(3) #self.begin = 1:2:3:None,self.end = 3:None test.push(4) #self.begin = 1:2:3:4:None,self.end = 4:None print(test.begin,test.end) 对整个程序来说都是如此,所以无论 self.begin = self.end 值是什么,self.end 值都是如此。但是,如果 self.begin 用于程序生命,那么为什么 else 块中的 self.begin = self.end 不也设置 self.end = node

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)