python node class
class Node(object):
def __init__(self, data=None, next_node=None):
self.data = data
self.next_node = next_node
python node class
class Node(object):
def __init__(self, data=None, next_node=None):
self.data = data
self.next_node = next_node
node class python
You need to find the last node without a .nextEl pointer and add the node there:
def add(self, newNode):
node = self.firstNode
while node.nextEl is not None:
node = next.nextEl
node.nextEl = newNode
Because this has to traverse the whole list, most linked-list implementations also keep a reference to the last element:
class List(object):
first = last = None
def __init__(self, fnode):
self.add(fnode)
def add(self, newNode):
if self.first is None:
self.first = self.last = newNode
else:
self.last.nextEl = self.last = newNode
Because Python assigns to multiple targets from left to right, self.last.nextEl is set to newNode before self.last.
Some style notes on your code:
Use is None and is not None to test if an identifier points to None (it's a singleton).
There is no need for accessors in Python; just refer to the attributes directly.
Unless this is Python 3, use new-style classes by inheriting from object:
class Node(object):
# ...
python node
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data
def PrintTree(self):
print(self.data)
root = Node(10)
root.PrintTree()
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us