Python 的双向链表
Doubly linked list with Python
我正在尝试在 python 中实现双向链表,但无法正确实现删除。
def deleteFromBeginning(self):
if self.listLength()==0:
print("Can't delete from an empty list")
else:
self.head = self.head.next
def deleteFromEnd(self):
if self.listLength() ==0:
print("Can't delete from an empty list")
else:
current=self.head
while(current.next!=None):
current.prev = current
current = current.next
current.prev.next = None
current.prev = None
def deleteFromPosition(self,pos):
if pos> self.listLength() or pos<0:
ValueError("Enter a valid position")
elif pos==0:
deleteFromBeginning()
elif pos == self.listLength():
deleteFromEnd()
else:
if self.listLength()==0:
print("Can't delete from an empty list")
else:
current=self.head
count = 1
while(current.next!=None and count<pos):
current = current.next
count = count+1
current.prev.setNext(current.next)
current.next.setPrev(current.prev)
current.setPrev(None)
current.setNext(None)
def deleteWithData(self,data):
if self.listLength()==0:
ValueError("Can't delete from an empty list")
else:
current = self.head
while current.next!=None:
if current.data == data:
current.prev.next = current.next
current.next.prev = current.prev
return
else:
current = current.next
print("The value provided is not present")
从头到尾的删除工作正常,但删除位置和数据没有给出预期的结果。
我要做的一个轻微重构是移动你的
if self.listLength()==0:
print("Can't delete from an empty list")
检查到最顶部,而不是在 else 内部。这应该是您最先进行的检查之一。
除此之外,您可以尝试跟踪前一个节点:
while(current.next!=None and count<pos):
temp = current
current = current.next
count = count+1
temp.setNext(current.next)
current.next.setPrev(temp)
这应该通过将前一个节点重新链接到下一个节点来删除 x 位置的节点。
还没有测试过,让我知道它是怎么回事!
我正在尝试在 python 中实现双向链表,但无法正确实现删除。
def deleteFromBeginning(self):
if self.listLength()==0:
print("Can't delete from an empty list")
else:
self.head = self.head.next
def deleteFromEnd(self):
if self.listLength() ==0:
print("Can't delete from an empty list")
else:
current=self.head
while(current.next!=None):
current.prev = current
current = current.next
current.prev.next = None
current.prev = None
def deleteFromPosition(self,pos):
if pos> self.listLength() or pos<0:
ValueError("Enter a valid position")
elif pos==0:
deleteFromBeginning()
elif pos == self.listLength():
deleteFromEnd()
else:
if self.listLength()==0:
print("Can't delete from an empty list")
else:
current=self.head
count = 1
while(current.next!=None and count<pos):
current = current.next
count = count+1
current.prev.setNext(current.next)
current.next.setPrev(current.prev)
current.setPrev(None)
current.setNext(None)
def deleteWithData(self,data):
if self.listLength()==0:
ValueError("Can't delete from an empty list")
else:
current = self.head
while current.next!=None:
if current.data == data:
current.prev.next = current.next
current.next.prev = current.prev
return
else:
current = current.next
print("The value provided is not present")
从头到尾的删除工作正常,但删除位置和数据没有给出预期的结果。
我要做的一个轻微重构是移动你的
if self.listLength()==0:
print("Can't delete from an empty list")
检查到最顶部,而不是在 else 内部。这应该是您最先进行的检查之一。
除此之外,您可以尝试跟踪前一个节点:
while(current.next!=None and count<pos):
temp = current
current = current.next
count = count+1
temp.setNext(current.next)
current.next.setPrev(temp)
这应该通过将前一个节点重新链接到下一个节点来删除 x 位置的节点。
还没有测试过,让我知道它是怎么回事!