簡體   English   中英

單鏈表 Python

[英]Singly Linked List Python

這是錯誤,它是我在終端中運行的代碼的結果

谷歌協作輸出在這里

我的代碼有問題,誰能找到此代碼的解決方案?

class Node:
# Node untuk singly Linkedlist
def __init__(self, data):
    self.data = data
    self.next = data
    
class singly_linked_list:
def __init__(self):
    # Membuat list kosong
    self.head = None
    self.tail = None
    self.count = 0
    
def print_all_item(self):
    # Iterate the list.
    current_item = self.head
    while current_item:
        val = current_item.data
        current_item = current_item.next
        print(val)
        
def append_item(self, data):
    #menambahkan item pada list
    node = Node(data)
    if self.tail:
        self.tail.next = node
        self.tail = node
    else:
        self.head = node
        self.tail = node
    self.count += 1        

測試 :

listPertama = singly_linked_list()
listPertama.append_item('PHP')
listPertama.append_item('Python')
listPertama.append_item('C#')
listPertama.append_item('C++')
listPertama.append_item('Java')
listPertama.print_all_item()

預期結果 :

PHP
Python
C#
C++
Java

只需將第五行中的“數據”更改為“無”即可。

像這個類節點:

# Node untuk singly Linkedlist
    def __init__(self, data):
        self.data = data
        self.next = None

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM