Home > Net >  TypeError: missing 1 required positional argument (LinkedList)
TypeError: missing 1 required positional argument (LinkedList)

Time:06-20

The problem I have is with my linked list implementation, it simply does not want to recognize my argument. Let me show you the code to illustrate what I mean:

class Node:
        def __init__(self, element:int):
            self.element = element
            
        element:int = 0
        nextNode = None

class LinkedList:
    head:Node = None
        
    def insert(self, element:int):
        if self.head == None:
            currentNode = Node.__init__(element)
            self.head = currentNode
        else:
            currentNode = self.head
            while currentNode.nextNode != None:
                currentNode = currentNode.nextNode
            newNode = Node.__init__(element)
            currentNode.nextNode = newNode 
    
    def prettyPrint(self):
        currentNode = self.head
        
        print("Current Linked List\n")
        while currentNode.nextNode != None:
            print(currentNode.element " ---> ")
            currentNode = currentNode.nextNode
        
                
            

def main():
    Linkedlist = LinkedList()
    Linkedlist.insert(1)
    Linkedlist.insert(9)
    Linkedlist.insert(2)
    Linkedlist.insert(18)
    Linkedlist.insert(5)
    Linkedlist.insert(8)
    Linkedlist.prettyPrint()
    


if __name__ == '__main__':
    main()

The error happens in the insert method at

currentNode = Node.init(element)

I'm new to Python and so any help is appreciated.

CodePudding user response:

Here is your code with two small fixes:

  1. Construct a Node by calling Node(element)
  2. in print either separate arguments with a , or use end="something" to tell print to put a "something" at the end of the output.
class Node:
    def __init__(self, element: int):
        self.element = element

    element: int = 0
    nextNode = None


class LinkedList:
    head: Node = None

    def insert(self, element: int):
        if self.head == None:
            currentNode = Node(element)
            self.head = currentNode
        else:
            currentNode = self.head
            while currentNode.nextNode != None:
                currentNode = currentNode.nextNode
            newNode = Node(element)
            currentNode.nextNode = newNode

    def prettyPrint(self):
        currentNode = self.head

        print("Current Linked List\n")
        while currentNode.nextNode != None:
            print(currentNode.element, end=" ---> ")
            currentNode = currentNode.nextNode


def main():
    Linkedlist = LinkedList()
    Linkedlist.insert(1)
    Linkedlist.insert(9)
    Linkedlist.insert(2)
    Linkedlist.insert(18)
    Linkedlist.insert(5)
    Linkedlist.insert(8)
    Linkedlist.prettyPrint()


if __name__ == "__main__":
    main()

Have fun learning python ;)

CodePudding user response:

As I mentioned in the comment, replace currentNode = Node.__init__(element) with currentNode = Node(element).

Also, I would recommend to change your Node class to something like this.

class Node:
        def __init__(self, element:int):
            self.element = element
            self.nextNode = None

Furthermore there are another issues in your prettyPrint() method.

First, you will get TypeError because of line, print(currentNode.element " ---> ").

Second, you are not printing the last element of the linked list. So, add print(currentNode.element) after the while loop.

So, I would change it to something like this to get your desired output.

def prettyPrint(self):
        currentNode = self.head
    
        print("Current Linked List\n")
        while currentNode.nextNode != None:
            print(currentNode.element, end=' ---> ')
            currentNode = currentNode.nextNode
        print(currentNode.element)
  • Related