链表阻塞蟒蛇



我在链表上有一个作业,游戏的名称是将字符串中的单词添加到实现的链表中,该数组按指针的字母顺序排列。

但是,当我运行以下代码时,Python Shell 卡住了,没有结果返回。请帮忙!谢谢!

我认为错误可能在这里,但我不确定为什么没有结果返回:/

if self.NextFree != len(self.array):
prev = curr = self.Start
while self.array[curr].word < nword:
prev = curr
curr = self.array[curr].pointer

完整的代码在这里:(对不起,如果它是一个初学者!:)(

class ListNode:
def __init__(self,word,pointer,count = 1):
self.word = word
self.pointer = pointer
self.count = count

class LinkedList:
def __init__(self):
self.array = []
self.Start = -1
self.NextFree = self.Start + 1
def Initialise(self):
MAX = 30
for i in range(MAX-1):
self.array.append(ListNode('',i+1))
self.array.append(ListNode(' ', -1))     #last node has pointer of -1
def Display(self):
print(self.Start, self.NextFree, len(self.array),sep = ' ')
print("Index  ", "Content  ", "Count  ", "Pointer")
for i in range(len(self.array)):
print('{0:<7}{1:<11}{2:<8}{3:}'.format(i, self.array[i].word, self.array[i].count, self.array[i].pointer))
def IsEmpty(self):
return self.Start == -1
def IsFull(self):
return self.NextFree == len(self.array)
def Update(self,nword):        #nword = newword      
if self.IsEmpty(): 
self.Start = 0
self.array[0].word = nword
self.NextFree += 1
else:
if self.NextFree != len(self.array):
prev = curr = self.Start
while self.array[curr].word < nword:
prev = curr
curr = self.array[curr].pointer
if self.array[curr].word == nword:
self.array[curr].count += 1
else:
self.array[prev].pointer = self.NextFree
self.array[self.NextFree].word = nword
self.array[self.NextFree].pointer = curr
self.NextFree += 1
else:
print ("f")
def DisplayT(self):
for i in range(len(self.array)):
print(self.array[i].word)
l = LinkedList()
l.Initialise()
##with open("Story.txt") as file:
##    for words in file.readline().split(" "):   #story.txt is single line
##        l.Update(words)
story = "she sells seashells by the seashore the shells she sells are surely seashells so if she sells shells on the seashore i am sure she sells seashore shells peter piper picked a peck of pickled peppers did peter piper pick a peck of pickled peppers if peter piper picked a peck of pickled peppers where is the peck of pickled peppers peter piper picked"
for words in story:
l.Update(words)
l.Display()

因为这是一个作业问题。我会尽力引导你找到答案!

请在您的代码中替换它(在更新功能上(,看看打印出什么 =(

while self.array[curr].word < nword:
print(self.array[curr].word)
print(nword)
print('loop')
prev = curr
curr = self.array[curr].pointer

尝试通过检查此打印并在此处发表评论来意识到此while循环的问题所在。到那时我会用更好的答案进行编辑!

但基本上你陷入了一个无限循环。试着找出原因!

相关内容

  • 没有找到相关文章

最新更新