Mohammed Fahad Kaleem Mohammed Fahad Kaleem - 2 months ago 20
Python Question

Linked List Type Error

I am a beginner and I started learning python programming and I am stuck with an error. I get a type error

class Node:

def __init__(self):
self.data = None
self.next = Node

def setData(self,data)
self.data = data

def getData(self):
return self.data

def setNext(self,next):
self.next = next

def getNext(self):
return self.next

def hasNext(self):
return self.next!=None


class LinkedList(object):

def __init__(self):
self.length = 0
self.head = None

def listLength(self):
currentNode = self.head
length = 0

while currentNode.hasNext:
length = length + 1
currentNode = currentNode.getNext()
return length

"""
Methods to Insert nodes in a Linked List:
# insertNode: Use this method to simply insert a node to the Linked List
# insertHead: Use this method to insert a node at the head of the Linked List
# insertTail: Use this method to insert a node at the tail of the Linked List
# insertAtPosition: Use this method to insert a node at a particular position of the Linked List
"""

def insertNode(self,node):
if self.length == 0:
self.insertHead(node)
else:
self.insertTail(node)

def insertHead(self, data):
nodeToBeInserted = Node()
nodeToBeInserted.setData(data)
if self.length == 0:
self.head = nodeToBeInserted
else:
nodeToBeInserted.setNext(self.head)
self.head = nodeToBeInserted
self.length = self.length + 1

def insertTail(self,data):
nodeToBeInserted = Node()
nodeToBeInserted.setData(data)
currentNode = self.head

while currentNode.getNext() != None:
currentNode = currentNode.getNext()
currentNode.setNext(nodeToBeInserted)
self.length = self.length + 1

def insertAtPosition(self,data, position):
if position > self.length or position < 0:
print("Invalid position!, The size of the Linked List is:%s"%self.length)
else:
if position ==0:
self.insertHead(data)
else:
nodeToBeInserted = Node()
nodeToBeInserted.setData(data)
currentNode = self.head
count = 0
while count < position - 1:
currentNode = currentNode.getNext()
count = count + 1
nodeToBeInserted.setNext(currentNode.getNext())
currentNode.setNext(nodeToBeInserted)
self.length = self.length+1

ll = LinkedList()
ll.insertNode(1)
ll.insertNode(2)
ll.insertNode(3)


The error I am seeing is:

Traceback (most recent call last):
File "C:/Users/XPS 13/PycharmProjects/DataStructures/LinkedList/Implementation/SinglyLinkedList.py", line 154, in <module>
ll.insertNode(2)
File "C:/Users/XPS 13/PycharmProjects/DataStructures/LinkedList/Implementation/SinglyLinkedList.py", line 92, in insertNode
self.insertTail(node)
File "C:/Users/XPS 13/PycharmProjects/DataStructures/LinkedList/Implementation/SinglyLinkedList.py", line 121, in insertTail
while currentNode.getNext() != None:
TypeError: getNext() missing 1 required positional argument: 'self'

Process finished with exit code 1


If someone can please explain me the reason for this error it will be appreciated.

Thank you.

Answer

You have made mistakes in your code:

In Node class:

self.next = Node

Why do you make this statement? Assign class itself to next is unreasonable. it should be:

self.next = None

Another thing, I would like to suggest is you'd better design a good __init__ function for each class. Think the __init__ function in a class as a constructor for the class.

In your __init__ function, no arguments are provided, which means when you instantiate the class, you cannot pass any data to it. In practice, we always need to provide some data to instantiate a class. Let's take the Node class for example, when we use it, we often already know the data this node should store, and the next node information, may or may not be known. So the better way to define its __init__ function is:

def Node():
    def __init__(self, data, next=None):
        self.data = data
        self.next = next

Next time, you make new instance with data property known:

node = Node(data=1)

If you also know the next property:

node = Node(data=1, next=next)

This make you more convenient, without need to call setX function.