将元素插入双链表python中

时间:2017-07-01 00:34:22

标签: python

我正在尝试编写插入函数。这是我到目前为止,但它只输出[15]。我主要与插入函数中if和elif语句中的节点所引用的内容相混淆。

class Container:
    class node:
        def __init__( self, value, nextNode, prevNode ):
            self.value = value
            self.next = nextNode
            self.prev = prevNode

    def __init__( self ):           
        self.top = self.last = None
        self.size = 0

    def __len__( self ):
        return self.size

    def _locate( self, val ):
        currentNode=self.top
        while True:
            if currentNode is None:
                return None, False
            # code the rest of _locate. Make a complete case analysis
            elif currentNode.value == val:
                return currentNode, True
            elif currentNode.value>val:
                return currentNode, False
            elif currentNode.next == None:
                return None, False
            else:
                currentNode = currentNode.next


    #Code the three functions     
    def find( self, val ): 
        temp = val 
        if self._locate(temp) is True:
            return True
        else:
            return False

    def insert( self, val ):
        #use _locate. If found=True, do nothing
        # If found=False and node is not None,
        #   then insert a new node just before the returned node
        # If found=False and node is None
        #   then insert a new node at the last pointer
        # And change size
        # Complete the function code in the four cases below

        node, found = self._locate(val) 

        if found==False:

            newNode = self.node(val, None, None)

            if node is not None and node.prev is not None:
                newNode.next = newNode

            elif node is not None and node.prev is None:
                newNode.next = newNode

            elif node is None and self.last is not None:
                self.prev = self.top
                selt.top = self.top.next
            elif node is None and self.last is None:
                self.top = newNode

    def delete( self, val ):
        # Use _locate again.
        # Delete the returned node if found is True
        #    in the four cases below
        node, found = self._locate(val)
        '''
        if found:
            if node.prev is not None and node.next is not None:
                node.prev = node.next #trying to connect node.prev pointer to node.next pointer
            elif node.prev is not None and node.next is None:
                self.last = node.prev #make the previous node the last node
            elif node.prev is None and node.next is not None:
                self.top = node.next #current node is first node, change it so next node is the new first node
            elif node.prev is None and node.next is None:
                self.size = 0 #there's only one node in list, change the size so now there's zero
                '''



#For code check
c = Container()
c.insert(10)
c.insert(4)
c.insert(2)
c.insert(15)
c.delete(4)
c.insert(8)
c.insert(9)
c.delete(10)
check=[]
pointer = c.top

while pointer is not None:
   check.append(pointer.value)
   pointer = pointer.next
print(check)
print(c.find(8), c.find(10))
# The output should be
#   [2, 8, 9, 15]
#   True False

1 个答案:

答案 0 :(得分:0)

  • 你称之为插入功能'插入时没有提及排序,但是您的预期输出显示为排序输出。我已经进行了简单的插入(即没有排序)。
  • 正如@chepner所提到的,您的查找功能会调用“定位”来查找'返回一个元组,但是你将这个元组与一个值(即True或False)进行比较

    class Container_insert_notSorted:
    
        class node:
            def __init__( self, value, nextNode, prevNode ):
                self.value = value
                self.next = nextNode
                self.prev = prevNode
    
        def __init__( self ):           
            self.top = self.last = None
            self.size = 0
    
        def __len__( self ):
            return self.size
    
        def _locate( self, val ):
            currentNode=self.top
            while True:
                if currentNode is None:
                    return None, False
                elif currentNode.value == val:
                    return currentNode, True
                elif currentNode.next == None:
                    return None, False
                else:
                    currentNode = currentNode.next
    
            return None, False
    
    
        #Code the three functions     
        def find( self, val ): 
            node, found = self._locate(val)
            return found
    
    
        def insert( self, val ):
            newNode = self.node(val, None, None)
    
            if self.last is None:
                self.top = newNode
                self.last = newNode
    
            else:
                node, found = self._locate(self.last.value)
                if found:
                    newNode.prev = node.value
                    node.next = newNode
                    self.last = newNode
    

逻辑是

  1. 首次运行insert时,创建一个新节点,其prev和next指针为null;将self.top设置为新节点;将self.last设置为新节点。
  2. 后续运行

    • 创建新节点
    • 找到您的最后一个节点(即找到self.last.value)= node
    • node.next应指向刚创建的新节点
    • 然后覆盖lastnode,使其现在指向此新节点