I have written some code that contains values of players and their scores within an SLL data structure. All works fine until I try to delete a node by inputting a players ID value and then output the modified SLL without that players score.
Bellow is my 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
Main body code:
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()
My question is where am I going wrong within delete_node? I do think that code is working up until to the point it has to print the new SLL but I can't workout where. Maybe I've overlooked something and just need a fresh pair of eyes to point out my silly mistake.
Just a note I am relative new to the concept of data structures and algorithms so please excuse my ignorance if I've done something the wrong way or the question does not make sense.
Thank you for the help and feedback.