打印正在输出链表元素而无需循环

huangapple go评论85阅读模式
英文:

Print is printing elements of linked list without loop

问题

我正在创建链表。在我的链表中,我在Node和LinkedList两个类中都有__str__方法。我尝试的目标是,每当我将对象打印到控制台时,以可读格式打印对象。因此,我在Node类中使用了__str__方法,但我不知道为什么每当我使用print()函数时,它会在不使用任何循环的情况下打印链表中的所有元素。

我发现在Node类的__str__方法中使用{self.next}是导致这种行为的原因,但我不知道它是如何工作的。

英文:

I am creating linked list. In my linked list i have str method in both classes Node and LinkedList. What i was trying to do is, print object in readable format whenever i print object to console. So i used str method in Node class, but i have no idea how this is printing all elements in the linked list without any loop whenever i use print() function.

class LinkedList:
    def __init__(self):
        self.head = None
    
    def __str__(self):
        if self.head is not None:
            return str(self.head)
        return 'empty linked list'
    
    def insert(self, data):
        if self.head is None:
            self.head = Node(data)
        else:
            head = self.head
            while head.next is not None:
                head = head.next
            head.next = Node(data)
        

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
    
    def __str__(self):
        return f"{self.data}\n{self.next}"
        
        
elems1 = [1,2,3,4]

llist1 = LinkedList()

for elem in elems1:
    llist1.insert(elem)
 
print(llist1)
    

I figured out that using {self.next} in str method in Node class is causing such behaviour but i have now idea how this is working.

答案1

得分: 1

  1. print(llist1) 调用方法 LinkedList.__str__
  2. LinkedList.__str__ 方法然后调用 str(self.head),然后调用方法 Node.__str__
  3. Node.__str__ 方法返回表达式 f"{self.data}\n{self.next}"
  4. 在评估该表达式时,您必须调用 self.next 然后将其字符串化。 (在该 f-string 中放置 self.next 表达式调用 __str__ 方法)
  5. 现在您已经在 self.next 上调用了 Node.__str__,我们知道这将继续调用 它的 self.next 依此类推,直到 self.next == None 为止,此时没有更多的 __str__ 方法可供调用
  6. self.next 将返回空字符串 并停止递归调用栈

您已经创建了一个递归程序而没有意识到它。

英文:

Lets go down (or up if you prefer) the call stack.

  1. print(llist1) this calls the method LinkedList.__str__
  2. that LinkedList.__str__ then calls str(self.head) which then calls the method Node.__str__
  3. that Node.__str__ then returns the expression f"{self.data}\n{self.next}"
  4. in evaluating that expression, you then must call the self.next and then stringifyit. (putting that expression self.next in that f-string calls the __str__ method)
  5. so now you've called Node.__str__ on self.next which as we know will then call its self.next and so on and so on until self.next == None in which case there's no more __str__ method to call
  6. self.next will return the empty string and stop the recursive call stack

You've created a recursive program without realizing it.

huangapple
  • 本文由 发表于 2023年7月3日 09:21:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定