2

This is my node:

class Node(object):
def __init__(self, data, next = None):
  self.data = data
  self.next_node = next
def get_next(self):
  return self.next_node
def set_next(self, next):
  self.next_node = next
def get_data(self):
  return self.data
def set_data(self):
  self.data = data

And this is the LinkedList itself:

class LinkedList(object):
def __init__(self, root = None):
  self.root = root
  self.size = 0
def size(self):
  return self.size
def insert(self, data):
  new_node = Node (data, self.root)
  self.root = new_node
  self.size += 1
def delete(self, data):
  this_node = self.root
  prev_node = None
  while this_node:
    if this_node.get_data() == data:
      if prev_node:
        prev_node.set_next(this_node.get_next())
      else:
        self.root = this_node
      self.size -= 1
      return True
    else:
      prev_node = this_node
      this_node = this_node.get_next()
  return False
def search(self, data):
  this_node = self.root
  while this_node:
    if this_node.get_data() == data:
      return data
    else:
      self.root = this_node.get_next()
    return None
def printLL(self):
  this_node = self.root
  while this_node:
    print(this_node.data)
    this_node  = this_node.get_next()

Finally, these are the tests I'm performing:

ll = LinkedList()
ll.insert(1)
ll.insert(2)
ll.printLL()
ll.delete(2)
ll.printLL()
if ll.search(2):
    print("Value 2 found")
else:
    print("Value 2 not found")
if ll.search(1):
    print("Value 1 found")
else:
    print("Value 1 not found")
ll.insert(4)
ll.printLL()
print(str(ll.size()))

EDIT: OK I've edited the code and it's no longer looping, however, I am now getting this output:

2
1
2
1
Value 2 found
Value 1 not found
4
1
Traceback (most recent call last):
  File "C:\Users\ErikIngvoldsen\Documents\Python Code\TestCode.py", line 71, in <module>
    print(str(ll.size()))
TypeError: 'int' object is not callable

Why is the object not callable? Also, why isn't my delete function working? For reference, this is what my output SHOULD look like:

2 1
1
Value 2 not found
Value 1 found
4 1
2

There's also the formatting problem, but for now I'll just focus on getting this to work properly.

2
  • Try changing self.root = this_node.get_next() into this_node = this_node.get_next() (you are looping through this_node but you never change its value). Commented Sep 3, 2017 at 22:30
  • You should accept an answer and start a new question. Also, your problem is that in your __init__() you redeclare self.size = 0 Commented Sep 4, 2017 at 3:47

2 Answers 2

1

The issue is that in your loop you use the condition while this_node and you never change the value of this_node.

You should assign this_node to the value of the next node until the end of yur LinkedList, your printLL() function should be:

def printLL(self):
  this_node = self.root
  while this_node:
    print(this_node.data)
    this_node  = this_node.get_next() 
Sign up to request clarification or add additional context in comments.

Comments

1

Your printLL function never changes the value of this_node, so the loop never terminates. You probably meant this_node = this_node.get_next().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.