在节点之间插入而不插入

在节点之间插入而不插入

本文介绍了LinkedList-在节点之间插入而不插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在学习Python中的链接列表,但是在节点之间插入节点时遇到了麻烦.让我在下面发布我的代码,并解释我已经做过的事情以及我认为问题出在哪里.

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

  • 好的,所以我想在20到30之间插入节点(25).
  • 在我的方法insert_in_between中,我采用两个参数:data和prev_data.数据为25,但由于将其传递给Node类而成为Node?但是prev_data是int(20).
  • 我期望这个打印语句在 thisval == prev_data 时打印 print("thisval == prev_data"),但我认为是因为节点和整数之间存在不匹配,那么该陈述将无法评估为真.
    • 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?

      编辑

      当我按照建议将行更改为: if thisval.data == prev_data:时,我得到一个错误:AttributeError:'int'对象没有属性'nextNode',它抱怨此行: new_node.nextNode = prev_data.nextNode

      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

      推荐答案

      上述建议不太奏效.我必须添加一个名为getNodes()的新方法来获取节点的索引,以便可以在我的insert_in_between方法中调用它.

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

      这篇关于LinkedList-在节点之间插入而不插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:28