我在python中实现了链表,它完美地添加了元素并打印了列表。 但我有删除方法的问题。我想删除列表中的最后一项
这是我的班级Cell():
class Cell():
def __init__(self, value = None, next = None):
self.value = value
self.next = next
这是我的类LinkedList():
class LinkedList():
def __init__(self):
self.top = None
self.last = None
self.length = 0
添加新Cell的方法效果很好:
def add (self, value, position = None):
newCell = Cell(value, None)
self.length += 1
if position is None or position >= self.length:
if self.top == None:
self.last = self.top = Cell(value, None)
else:
self.last.next = self.last = Cell(value, None)
elif position == 1:
self.top = Cell (value, self.top)
elif position > 1:
afterMe = self.top
i = 1
for i in range(position-2):
afterMe = afterMe.next
newCell.next = afterMe.next
afterMe.next = newCell
方法toString()也很好用:
def __str__(self):
linkedList = ''
cell = self.top
if self.top is None:
return 'Linked list is empty'
for i in range(self.length):
if cell == self.last:
linkedList += (str(cell.value))
else:
linkedList += (str(cell.value)+ ', ')
cell = cell.next
return linkedList
这是我的删除方法,它会产生错误:
def delete(self, value = None): # want to delete last cell
if self.top == None:
return None
current = self.top
if value is None:
self.length -= 1
while (current.next != self.last):
current = current.next
self.last = current
current.next = None
else:
while (current.next.value != value):
current = current.next
if current == self.last:
print ('no such value')
return
current.next = current.next.next
以下是代码的工作原理和错误:
numbers = LinkedList()
numbers.add(55)
numbers.add(75)
numbers.add(65)
print(numbers) # 55, 75, 65
numbers.add(3,2)
numbers.add (40,3)
print(numbers) # 55, 3, 40, 75, 65
numbers.delete()
print(numbers) # 55, 3, 40, 75
numbers.delete(40)
print(numbers)
''' returns error:
Traceback (most recent call last):
File "C:/Users/demin.va/Documents/Dropbox/Programming/Алгоритмы/связные списки.py", line 105, in <module>
print(numbers)
File "C:/Users/demin.va/Documents/Dropbox/Programming/Алгоритмы/связные списки.py", line 72, in __str__
linkedList += (str(cell.value)+ ', ')
AttributeError: 'NoneType' object has no attribute 'value'
'''
请回答,如何更改代码以正确删除最后一个单元格或从不同位置删除?
答案 0 :(得分:1)
current.next=None
应该在while循环之外:
def delete (self): #want to delete last cell
if self.top == None:
return None
self.length -=1
current = self.top
nextcell = self.top.next
while (nextcell != None):
if nextcell == self.last:
current = self.last
break
current = current.next
nextcell = nextcell.next
# current.next should be run only after finding the last element
# so place it outside the loop
current.next= None
答案 1 :(得分:0)
我不确定你的while循环在做什么,如果你想删除最后一个元素,请按照以下方式进行:
找到最后一个元素的下一个元素,将其旁边的元素设置为None,并指向该元素的最后一个元素:
def delete (self): #want to delete last cell
if self.top is None:
return None
if self.top == self.last:
self.top, self.last = None, None
self.length = 0
return
self.length -= 1
current = self.top
while current.next != self.last:
current = current.next
current.next = None
self.last = current
删除具有值的单元格。
以下是该算法的示例:
def delete_value(self, value):
if self.top == null: # empty list
return
predecessor, current = None, self.top
while current.value != value:
predecessor = current
current = current.next
if current is None: # not found, nothing to do:
return
self.length -= 1
if predecessor is None: # self.top == current, we are deleting the head of the linked list
self.top = self.top.next
else:
predecessor.next = current.next