Zevvysan
Zevvysan

Reputation: 303

Python LinkedList delete function not working

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.

Upvotes: 2

Views: 79

Answers (2)

Fady Saad
Fady Saad

Reputation: 1189

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() 

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599560

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

Upvotes: 1

Related Questions