python - 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())
解决方案
我建议您先阅读链接列表,您似乎并不完全了解它们的结构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())
推荐阅读
- javascript - CKEditor 数据不会在 Firestore 中上传 TypeError: text is not a function
- java - 应用 gradle 自定义模块插件
- php - DarkaOnLine / L5-Swagger 与 Datadog 冲突
- javascript - 如何使用for循环javascript找到n的平方
- ios - PencilKit 绘图有时会在黑暗模式下变为黑色
- pine-script - 根据净值损失百分比创建止损(回撤)
- gstreamer - 未能延迟将名为 rtspsrc0 的 GstRTSPSrc 的某个焊盘链接到名为 rtpjitterbuffer0 的 GstRtpJitterBuffer 的某个焊盘
- pyspark - 如果 sparksession 没有关闭会发生什么?
- python - Python Selenium Click Div 末尾有空格
- amazon-dynamodb - 如何在生产中运行的 dynamo DB 中引入新列?