Python 3。从链接列表中删除项目
我用python实现了链表,它完美地添加了元素并打印了链表。 但我有删除方法的问题。我想从列表中删除最后一项 这是我的班级单元() 这是我的类LinkedList(): 添加新单元格的方法效果良好:Python 3。从链接列表中删除项目,python,python-3.x,data-structures,linked-list,Python,Python 3.x,Data Structures,Linked List,我用python实现了链表,它完美地添加了元素并打印了链表。 但我有删除方法的问题。我想从列表中删除最后一项 这是我的班级单元() 这是我的类LinkedList(): 添加新单元格的方法效果良好: def add (self, value, position = None): newCell = Cell(value, None) self.length += 1 if position is None or position >= self.lengt
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
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
方法toString()也很有效:
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
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'
'''
numbers=LinkedList()
数字。添加(55)
数字。添加(75)
数字。添加(65)
打印(数字)#55、75、65
数字。添加(3,2)
数字。添加(40,3)
打印(数字)#55、3、40、75、65
删除()
打印(数字)#55、3、40、75
删除(40)
打印(数字)
''返回错误:
回溯(最近一次呼叫最后一次):
文件“C:/Users/demin.va/Documents/Dropbox/Programming/АПцццццзццццццццц109
打印(数字)
文件“C:/Users/demin.va/Documents/Dropbox/Programming/АПССцццзццзцццццзццц__
linkedList+=(str(cell.value)+',')
AttributeError:“非类型”对象没有属性“值”
'''
请回答,我应该如何更改代码以正确删除最后一个单元格或从不同位置删除?
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
我不确定while循环在做什么,如果要删除最后一个元素,请按以下方式执行:
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
如果我不想删除最后一个单元格,而是要删除有值的单元格呢?例如numbers.delete(40),我想删除值为40的单元格,如果我不想删除最后一个单元格,而是要删除值为40的单元格,该怎么办?例如number.delete(40),我希望删除值为40的单元格