首页 > 解决方案 > Python中的LinkedList数据结构解释

问题描述

我设法找到定义 LinkedList 类和其中所有事实上的方法的 python 代码,但完全无法弄清楚每一行代码的作用......有人可以评论它解释每一行的作用,以便我可以更好地掌握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())

标签: pythonlinked-listcomments

解决方案


我建议您先阅读链接列表,您似乎并不完全了解它们的结构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())

推荐阅读