I need to create a linked list in python as assessment for my university. Everything works fine, except for one little thing: the output shows "None" at the end. Example output 1, 5, 8, 9, None
How can I remove this None at the end? I tried many things but nothing, I don't know if the problem is the print_list() function or something else.
I test all the other functions, such as "size" and they work fine.
Please see my code below.
class Node():
def __init__(self,data):
self.data = data
self.next = None
def get_data(self):
return self.data
def get_next(self):
if self.next is not None:
return self.next
def set_data(self,newdata):
self.data = newdata
def set_next(self,newnext):
self.next = newnext
def __str__(self):
return str(self.data)
class UnorderedList():
def __init__(self):
self.head = None
def add(self,item):
tmp = Node(item)
tmp.set_next(self.head)
self.head = tmp
def isEmpty(self):
return self.head == None
def size(self):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.get_next()
return count
def print_list(self):
if self.head is None:
return
node = self.head
while node is not None:
print(node.__str__(), end = ' ')
node = node.get_next()
my_list = UnorderedList()
my_list.add(2)
my_list.add(1)
my_list.add(5)
my_list.add(0)
my_list.add(8)
my_list.add(9)
my_list.add(1)
my_list.add(8)
print(my_list.print_list())
So, out of this code, my expectation was:
8, 1, 9, 8, 0, 5, 1, 2
But what I get is:
8, 1, 9, 8, 0, 5, 1, 2, None
I would appreciate any help, thank you so much.