双链接列表打印



在我的任务中,我需要将char放入一个双链表中并打印它们。

我初始化了节点和DLL(+附加(:

class Node:
def __init__ (self, data):
self.data = data
self.next = None
self.prev = None
class DoublyLinkedList():
def __init__(self):
self.head =None
def append(self, data):
if self.head is None:
new_node = Node(data)
new_node.prev = None
self.head = new_node
else:
new_node = Node(data)
cur = self.head
while cur.next:
cur = cur.next
cur.next =new_node
new_node.prev = cur
new_node.next = None 

然后我必须制作用于打印的函数和将char放入DDL的函数:

def print(self):
current = self.head
if (self.head == None):
print ("List is empty")
return
while(current != None):
print(current.data)
current = current.next

def Lista(lista):
doubleLinked = DoublyLinkedList()
for element in lista:
doubleLinked.append(element)

这就是我如何调用这些函数。这部分我不能改变!

L = Lista(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) 
print(L)

我得到了这个错误:

<ipython-input-5-1c60a61c4137> in print(self)
33 def print(self):
34 
---> 35     current = self.head
36     if (self.head == None):
37         print ("List is empty")
AttributeError: 'NoneType' object has no attribute 'head'

在函数Lista中,您必须返回doubleLinked列表。

相关内容

  • 没有找到相关文章

最新更新