LinkedList - Insert Between Nodes not Inserting

巧了我就是萌 提交于 2019-12-23 05:45:10

问题


So I'm learning Linked Lists in Python but having trouble inserting a node between my nodes. Let me post my code below and explain what I've done and where I believe the problem is happening.

class Node(object):
    def __init__(self, data):
        self.data = data
        self.nextNode = None
'''
    def __str__(self):
      return str(self.data)
'''       
class LinkedList(object):
    def __init__(self):
        self.head = None
        self.tail = None


    # Insert inbetween
    def insert_in_between(self, data, prev_data):
      print("<<< INSERT INBETWEEN >>>")
      # instantiate the new node
      new_node = Node(data)
      print("This is new_node: ", new_node)
      # assign to head
      thisval = self.head
      print("This is thisval: ", thisval)
      print("This is prev_data: ", prev_data)
      # check each value in linked list against prev_data as long as value is not empty
      while thisval is not None:
        print("thisval is NOT NONE")
        print("in while loop, thisval = ", thisval)
        print("in while loop, prev_data = ", prev_data)
        # if value is equal to prev_data 
        if thisval == prev_data:
          print("thisval == prev_data")
          # make the next of new_node the prev_data's next
          new_node.nextNode = prev_data.nextNode
          # make the next of prev_data the new_node
          prev_data.nextNode = new_node
          break;
        # if value is not eqaul to prev_data then assign variable to next Node
        else:
          thisval = thisval.nextNode


    def push_from_head(self, NewVal):
      new_node = Node(NewVal)
      print("This is new_node: ", new_node.data)
      last = self.head
      print("This is last/HEAD: ", last)
      if self.head is None:
        print("Head is NONE")
        self.head = new_node
        print("This is self.head: ",self.head)
        return
      print("last.nextNode: ", last.nextNode)
      while last.nextNode is not None:
        print("this is last inside while loop: ", last.data)
        print("last.nextNode is not NONE")
        last = last.nextNode
        print("This is the last last: ", last.data)
      last.nextNode = new_node
      print("This is last.nextNode: ", last.nextNode)   


    def print_nodes(self):
        if self.head:
            thisval = self.head

            while thisval:
                print("This is node: ", thisval.data)
                thisval = thisval.nextNode

e1 = LinkedList()

e1.push_from_head(10)
e1.push_from_head(20)
e1.push_from_head(30)
e1.push_from_head(40)
e1.push_from_head(50)

e1.insert_in_between(25, 20)
e1.print_nodes() 
  • Ok, so I want to insert the node (25) in between 20 and 30.
  • In my method insert_in_between I'm taking two arguments: data and prev_data. Data is 25, but becomes a Node because I'm passing it into a Node class? But prev_data is an int(20).
  • I was expecting this print statment to print print("thisval == prev_data") when thisval == prev_data but I think because there a mismatch between nodes and ints, that statment won't evaluate to true.

I'm sure this is an easy fix and have been trying to work out a solution with no luck. Can anyone point me in the right direction?

EDIT

When I change the line as suggested to: if thisval.data == prev_data: I get an error: AttributeError: 'int' object has no attribute 'nextNode' where it complains about this line: new_node.nextNode = prev_data.nextNode


回答1:


You are checking to see if an integer is equal to a node. That will never occur because they are different data types. You need to check

if thisval.data == prev_data

This will compare the data stored in each node object to the entered integer value (20) until it is found. The rest of your code should function correctly in this case.




回答2:


The above suggestion didn't quite work. I had to add a new method called getNodes() to get the index of the node so I can call it in my insert_in_between method.

class Node(object):
    def __init__(self, data):
        self.data = data
        self.nextNode = None

class LinkedList(object):
    def __init__(self):
        self.head = None
        self.tail = None

    def getNode(self, index):
      if self.head is not None:
        current = self.head
        count = 0
        while(current):
          if count == index:
            return current;
          else:
            count+=1
            current = current.nextNode
        else:
          print("There are no nodes")


    # Insert inbetween
    def insert_in_between(self, data, prev_data):
      print("<<< INSERT INBETWEEN >>>")
      # instantiate the new node
      new_node = Node(data)
      print("This is new_node: ", new_node)
      # assign to head
      thisval = self.head
      print("This is thisval: ", thisval)
      print("This is prev_data: ", prev_data)
      prev_node = self.getNode(1)
      print("This is prev_node: ", prev_node.data)
      # check each value in linked list against prev_data as long as value is not empty

      while thisval is not None:
        print("thisval is NOT NONE")
        print("in while loop, thisval = ", thisval)
        print("in while loop, prev_data = ", prev_data)

        # if value is equal to prev_node 
        if thisval.data == prev_node.data:
          print("thisval == prev_node")
          # make the next of new_node the prev_node's next
          new_node.nextNode = prev_node.nextNode
          # make the next of prev_node the new_node
          prev_node.nextNode = new_node
          break;
        # if value is not eqaul to prev_data then assign variable to next Node
        else:
          thisval = thisval.nextNode

    def push_from_head(self, NewVal):
      new_node = Node(NewVal)
      print("This is new_node: ", new_node.data)
      last = self.head
      print("This is last/HEAD: ", last)
      if self.head is None:
        print("Head is NONE")
        self.head = new_node
        print("This is self.head: ",self.head)
        return
      print("last.nextNode: ", last.nextNode)
      while last.nextNode is not None:
        print("this is last inside while loop: ", last.data)
        print("last.nextNode is not NONE")
        last = last.nextNode
        print("This is the last last: ", last.data)
      last.nextNode = new_node
      print("This is last.nextNode: ", last.nextNode)   


    def print_nodes(self):
        if self.head:
            thisval = self.head

            while thisval:
                print("This is node: ", thisval.data)
                thisval = thisval.nextNode

e1 = LinkedList()

e1.push_from_head(10)
e1.push_from_head(20)
e1.push_from_head(30)
e1.push_from_head(40)
e1.push_from_head(50)

e1.insert_in_between(25, 20)
e1.print_nodes()


来源:https://stackoverflow.com/questions/53346094/linkedlist-insert-between-nodes-not-inserting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!