Home > Back-end >  Singly Linked List Python
Singly Linked List Python

Time:10-27

This is the error which is the result of the code that I have run in my terminal

Google Collab Output goes here

I have some issue with my code, anyone can find the solution of this code?

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        

Test :

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()

Expected Result :

PHP
Python
C#
C  
Java

CodePudding user response:

Just change the "data" in the fifth line to "None".

Like this class Node:

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