Python中的LinkedList数据结构解释

LinkedList data structure explanation in Python

我设法找到了 python 代码,它定义了一个 LinkedList class 和其中的所有事实上的方法,但完全无法弄清楚每一行代码的作用......有人可以发表评论吗在上面解释每一行的作用,这样我就可以更好地理解 python?

中的 LinkedLists
class Node:#what is the significance of a node
  def __init__(self, data, next):#why these parameters
    self.data = data
    self.next = next
class LinkedList:
    def __init__(self):#what is a head
        self.head = None
    
    def add_at_front(self, data):
        self.head = Node(data, self.head)      

    def add_at_end(self, data):
        if not self.head:#what is it checking 
            self.head = Node(data, None)
            return#what is it returning
        curr = self.head
        while curr.next:
            curr = curr.next
        curr.next = Node(data, None)

    def get_last_node(self):
        n = self.head
        while(n.next != None):
            n = n.next
        return n.data

    def is_empty(self):#i understand this method
        return self.head == None

    def print_list(self):#i also undertsnad this one
        n = self.head
        while n != None:#what is this loop doing
            print(n.data, end = " => ")
            n = n.next
        print()


s = LinkedList()
s.add_at_front(5)
s.add_at_end(8)
s.add_at_front(9)

s.print_list()
print(s.get_last_node())

我建议你先阅读链表,你似乎没有完全理解它们的结构https://www.javatpoint.com/singly-linked-list

class Node:#this node class is used to represent a node in the linked list
  def __init__(self, data, next):# data is the data carried by the node, next is a reference to the next node in the list
    self.data = data
    self.next = next
class LinkedList:
    def __init__(self):#the head is the first node of a linkedlist
        self.head = None
    
    def add_at_front(self, data):
        self.head = Node(data, self.head)      

    def add_at_end(self, data):
        if not self.head: #this checks if the list is empty, i.e the head node is None.  
                          #Here the new node is inserted at the head since the rest of the list is empty
            self.head = Node(data, None)
            return# this return is used to exit the function, it returns nothing
        curr = self.head
        while curr.next:
            curr = curr.next
        curr.next = Node(data, None)

    def get_last_node(self):
        n = self.head
        while(n.next != None):
            n = n.next
        return n.data

    def is_empty(self):#i understand this method
        return self.head == None

    def print_list(self):#i also undertsnad this one
        n = self.head
        while n != None: #this checks if we traversed through the whole linked list already
                        # this is what the LL looks like: HEAD->Node->Node->None
                        # if n has reached None, then that means it has successfully visited all the nodes in the list
            print(n.data, end = " => ") 
            n = n.next #this line jumps to the next node of the linked list
        print()


s = LinkedList()
s.add_at_front(5)
s.add_at_end(8)
s.add_at_front(9)

s.print_list()
print(s.get_last_node())

这是简单的 class 规则。直观地理解 class 节点作为节点块。

Class 节点视觉图像:https://i.stack.imgur.com/459GJ.jpg

现在节点包含两部分,datanext。数据包含数据,而下一部分包含下一个节点的地址。

现在每当初始化 LinkedList class 时,头指针(变量)将生成为 null 作为链表的基本概念。将 head 视为您的代理人,它将引导您从一个节点到另一个节点。

头部初始化视觉图像:https://i.stack.imgur.com/PUar7.jpg

现在在通过链表进行任何操作时,您都必须检查是否有您的代理。如果 head 在那里,那么您可以逐个节点并相应地执行您的操作。

下一个问题,是什么 returning

    def add_at_end(self, data):
    if not self.head:#what is it checking 
        self.head = Node(data, None)
        return#what is it returning
    curr = self.head
    while curr.next:
        curr = curr.next
    curr.next = Node(data, None)

看,这是一种if-else语句。在上面的代码中,如果没有 head 或 head==null,您将创建一个新节点。节点所有更改都结束了 class 不仅对方法。所以,return 将停止执行下一个方法语句。 如果条件不满足,curr = self.head开始执行。

关于循环,您的代理将引导您到达最后一个节点并为您获取每个节点数据。 最后打印更改行,仅此而已。

循环视觉图像:https://i.stack.imgur.com/vN2pE.jpg

谢谢