gpt4 book ai didi

Python从单向链表中移除节点并输出修改后的SLL

转载 作者:太空宇宙 更新时间:2023-11-04 04:42:22 24 4
gpt4 key购买 nike

我编写了一些代码,在 SLL 数据结构中包含玩家的值及其得分。一切正常,直到我尝试通过输入玩家 ID 值来删除节点,然后输出没有玩家得分的修改后的 SLL。

下面是我的类(class):

class PlayerScore:                          

def __init__(self, score, next = None):
self.length=len(score)-1
self.rep=self.__str(score)
self.score = score
self.next = next

def __str(self, score):
terms = ["(No: "+str(score[0])+ \
", Game1: "+str(score[1])+ \
", Game2: "+str(score[2]) + \
", Game3: "+str(score[3])]
return str(terms)
def __eq__(self, that):
return self.score[0] == that[0]

def delete_node(self, data):
curr = self
curr_score = curr.score
prev = None;
while curr is not None:
if curr_score == data:
if prev is not None:
prev.next = curr.next
else:
self = curr.next
prev = curr
curr = self.next
modi = curr_score
while(modi):
lyst = modi.marks
total = lyst[1]+lyst[2] +lyst[3]
print(" Student_ID.: " + str(lyst[0])+" A1: " + str(lyst[1])+" A2: "
+ str(lyst[2])+" Exam: " + str(lyst[3])+" ->total " + str(total))
modi = modi.next

主体代码:

def print_score(score_list):
if score_list is None:
print("No player records")
return
else:
print("Original linked list:")
curr = score_list
while curr is not None:
lyst = curr.score
print(" PlayerNo: " + str(lyst[0])+" Game 1: " + str(lyst[1])+" Game 2: "
+ str(lyst[2])+" Game 3: " + str(lyst[3]))
curr = curr.next
print()

def main(size = 4):

node1 = None

#create SLL
node2 = PlayerScore([199, 94, 96, 109], node1)
node3 = PlayerScore([185, 203, 156, 171], node2)
node4 = PlayerScore([173, 104, 190, 224], node3)
node5 = PlayerScore([154, 268, 287, 300], node4)

player_score_head = node5

print_score(player_score_head)

value = input("Enter a Student's ID for deletion: ")
print(' ')
player_score_head.delete_node(value)

if __name__ == "__main__":
main()

我的问题是我在 delete_node 中哪里出错了?我确实认为代码一直在工作,直到它必须打印新的 SLL,但我无法在哪里锻炼。也许我忽略了一些东西,只需要一双新的眼睛来指出我的愚蠢错误。

请注意,我对数据结构和算法的概念还比较陌生,所以如果我做错了什么或者问题没有意义,请原谅我的无知。

感谢您的帮助和反馈。

最佳答案

这是您的代码的修改版本,其中包含可正常运行的 delete_node 方法,我还对 PlayerScore 类进行了一些其他更改。我把身份证号码和分数数据分开了; ID 号不是分数,因此将它们组合起来没有任何意义。我给 PlayerScore 一个 __str__ 方法来使打印节点更容易。我摆脱了 __eq__ 方法,因为我发现它使代码更难阅读和分析。

我从 delete_node 中删除了 modi 内容;这些东西与节点删除无关,所以它属于一个单独的方法。

我还对 main 进行了一些更改。我给它一个输入循环,这样我们就可以尝试删除几个节点来测试我们的 delete_node 方法。并且我添加了一个测试来验证输入的 ID 号是否可以转换为整数。

class PlayerScore:
def __init__(self, data, nxt=None):
self.idnum, *self.scores = data
self.nxt = nxt

def __len__(self):
return len(self.scores)

def __str__(self):
terms = ["No: " + str(self.idnum)]
terms += ["Game {}: {}".format(i, v)
for i, v in enumerate(self.scores, 1)]
return ', '.join(terms)

def delete_node(self, idnum):
curr = self
prev = None
# Find the node with idnum
while curr is not None:
if curr.idnum == idnum:
break
prev = curr
curr = curr.nxt
else:
print("Node {} not found".format(idnum))
return self

#print('CURR', curr, 'PREV', prev)

if prev is None:
new_head = curr.nxt
else:
new_head = self
prev.nxt = curr.nxt

del curr
return new_head

def print_score(score_list):
if score_list is None:
print("No player records")
return

print("Original linked list:")
curr = score_list
while curr is not None:
print(curr)
curr = curr.nxt
print()

def main():
data_list = [
[199, 94, 96, 109],
[185, 203, 156, 171],
[173, 104, 190, 224],
[154, 268, 287, 300],
]

#create SLL
head = None
for data in data_list:
head = PlayerScore(data, head)

print_score(head)

while head is not None:
value = input("Enter a Student's ID for deletion, or 0 to exit: ")
try:
value = int(value)
except ValueError:
print("ID must be an integer")
continue
if value == 0:
break
head = head.delete_node(value)
print_score(head)


if __name__ == "__main__":
main()

演示输出

Original linked list:
No: 154, Game 1: 268, Game 2: 287, Game 3: 300
No: 173, Game 1: 104, Game 2: 190, Game 3: 224
No: 185, Game 1: 203, Game 2: 156, Game 3: 171
No: 199, Game 1: 94, Game 2: 96, Game 3: 109

Enter a Student's ID for deletion, or 0 to exit: abc
ID must be an integer
Enter a Student's ID for deletion, or 0 to exit: 200
Node 200 not found
Original linked list:
No: 154, Game 1: 268, Game 2: 287, Game 3: 300
No: 173, Game 1: 104, Game 2: 190, Game 3: 224
No: 185, Game 1: 203, Game 2: 156, Game 3: 171
No: 199, Game 1: 94, Game 2: 96, Game 3: 109

Enter a Student's ID for deletion, or 0 to exit: 199
Original linked list:
No: 154, Game 1: 268, Game 2: 287, Game 3: 300
No: 173, Game 1: 104, Game 2: 190, Game 3: 224
No: 185, Game 1: 203, Game 2: 156, Game 3: 171

Enter a Student's ID for deletion, or 0 to exit: 154
Original linked list:
No: 173, Game 1: 104, Game 2: 190, Game 3: 224
No: 185, Game 1: 203, Game 2: 156, Game 3: 171

Enter a Student's ID for deletion, or 0 to exit: 173
Original linked list:
No: 185, Game 1: 203, Game 2: 156, Game 3: 171

Enter a Student's ID for deletion, or 0 to exit: 185
No player records

关于Python从单向链表中移除节点并输出修改后的SLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50362021/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com