[英]Iterate over linked list with conditional node links
我對編碼的想法很陌生,所以我不能 100% 確定我在問什么。 我有一個對象列表,其中包含一個框的 ID、可選選項的數量以及與其鏈接的框的 ID。
例如,“Connect 6”總共有 3 個,“contect 7”總共有 2 個,“Connect 4”總共有 7 個
我有 2 個類 1 設置列表中的所有鏈接,然后應該遍歷它們的 1 個類。 我還沒有能夠得到總和部分,因為我所做的任何事情都會永遠打印出拳頭盒 ID,直到它崩潰 VS(不知道如何在它開始運行后阻止它做我的蠢事,即使關閉終端也不會)幫助)
class Node:
def __init__(self, box_id):
self.box_id = box_id
self.options_number = None
self.pre = None
self.next = None
def __repr__(self):
return self.box_id
class LinkedList():
def __init__(self, nodes=None):
self.head = None
self.reg_num = 0
if nodes is not None:
for node in nodes:
box_id = node.box_id
# print(f'One: {box_id}')
# box_id = node.next
# print(f'Two: {box_id}')
# if box_id == []:
# print("I should really stop")
while box_id != []:
print(box_id)
box_id = node.next
這是打印出節點的樣子。
nodes = node_setup(test)
for node in nodes:
print(f'Question Text: {node.name} Current: {node.box_id} Options: {node.options_number} Next Link: {node.next}')
Question Text: Connect 1 Current: {30706B51-789C-4AFA-998D-759297B95F99} Options: 1 Next Link: ['{EA65B6DA-90FE-412A-AF1F-83F994CCB5FA}']
Question Text: Connect 2 Current: {EA65B6DA-90FE-412A-AF1F-83F994CCB5FA} Options: 3 Next Link: ['{77AC8029-95D8-4DA8-9B62-D487DBA742D6}']
Question Text: Connect 3 Current: {77AC8029-95D8-4DA8-9B62-D487DBA742D6} Options: 1 Next Link: ['{DF56E1B2-DD19-485D-B720-A5A28AA6C156}']
Question Text: Connect 5 Current: {B4045D8D-6E54-4E43-B820-4C70036F08AF} Options: 1 Next Link: ['{D1720CEC-81FF-4460-A5CB-86BC9B5AB7F5}']
Question Text: Connect 4 Current: {DF56E1B2-DD19-485D-B720-A5A28AA6C156} Options: 2 Next Link: ['{B4045D8D-6E54-4E43-B820-4C70036F08AF}', '{8C51E3B4-7302-4453-AEE0-7C9E659CA2B0}']
Question Text: Connect 6 Current: {8C51E3B4-7302-4453-AEE0-7C9E659CA2B0} Options: 2 Next Link: ['{D837ECDC-897D-4A91-89E3-F6222FE12F06}']
Question Text: Connect 7 Current: {D1720CEC-81FF-4460-A5CB-86BC9B5AB7F5} Options: 1 Next Link: ['{D837ECDC-897D-4A91-89E3-F6222FE12F06}']
Question Text: Connect 8 Current: {D837ECDC-897D-4A91-89E3-F6222FE12F06} Options: 1 Next Link: []
在這里它正在運行 LinkedList
for node in nodes:
print(f'Question Text: {node.name} Current: {node.box_id} Options: {node.options_number} Next Link: {node.next}')
plz_Work = LinkedList(nodes)
Question Text: Connect 1 Current: {30706B51-789C-4AFA-998D-759297B95F99} Options: 1 Next Link: ['{EA65B6DA-90FE-412A-AF1F-83F994CCB5FA}']
Question Text: Connect 2 Current: {EA65B6DA-90FE-412A-AF1F-83F994CCB5FA} Options: 3 Next Link: ['{77AC8029-95D8-4DA8-9B62-D487DBA742D6}']
Question Text: Connect 3 Current: {77AC8029-95D8-4DA8-9B62-D487DBA742D6} Options: 1 Next Link: ['{DF56E1B2-DD19-485D-B720-A5A28AA6C156}']
Question Text: Connect 5 Current: {B4045D8D-6E54-4E43-B820-4C70036F08AF} Options: 1 Next Link: ['{D1720CEC-81FF-4460-A5CB-86BC9B5AB7F5}']
Question Text: Connect 4 Current: {DF56E1B2-DD19-485D-B720-A5A28AA6C156} Options: 2 Next Link: ['{B4045D8D-6E54-4E43-B820-4C70036F08AF}', '{8C51E3B4-7302-4453-AEE0-7C9E659CA2B0}']
Question Text: Connect 6 Current: {8C51E3B4-7302-4453-AEE0-7C9E659CA2B0} Options: 2 Next Link: ['{D837ECDC-897D-4A91-89E3-F6222FE12F06}']
Question Text: Connect 7 Current: {D1720CEC-81FF-4460-A5CB-86BC9B5AB7F5} Options: 1 Next Link: ['{D837ECDC-897D-4A91-89E3-F6222FE12F06}']
Question Text: Connect 8 Current: {D837ECDC-897D-4A91-89E3-F6222FE12F06} Options: 1 Next Link: []
['{EA65B6DA-90FE-412A-AF1F-83F994CCB5FA}']
['{EA65B6DA-90FE-412A-AF1F-83F994CCB5FA}']
['{EA65B6DA-90FE-412A-AF1F-83F994CCB5FA}']
['{EA65B6DA-90FE-412A-AF1F-83F994CCB5FA}']
['{EA65B6DA-90FE-412A-AF1F-83F994CCB5FA}']
ect....
老實說,我對如何解決這個問題感到非常迷茫,我將不勝感激任何人都可以提供的幫助
我現在已經解決了。 我在錯誤的地方使用了 for。
它仍然不完美,因為我無法讓它在有 2 個鏈接的地方工作,但現在這讓我超越了永遠循環,能夠解決其余的問題
class LinkedList():
def __init__(self, nodes=None):
self.head = None
self.reg_num = 0
if nodes is not None:
box_id = nodes[0].box_id
while box_id != []:
node_range = len(nodes)
for node in range(node_range):
if box_id == nodes[node].box_id:
name = nodes[node].name
print(f'name {name}')
next_id = nodes[node].next
if next_id != []:
next_id = next_id[0] #This needs to be something more clever
box_id = next_id
和版畫
name Connect 1
name Connect 2
name Connect 3
name Connect 4
name Connect 5
name Connect 7
name Connect 8
================================================== =
編輯:現在弄清楚如何做整件事
start_id = nodes[0].box_id
plz_Work = LinkedList(nodes, start_id)
班級
class LinkedList():
def __init__(self, nodes, start_id):
if nodes is not None:
box_id = start_id
while box_id != []:
node_range = len(nodes)
for node in range(node_range):
if box_id == nodes[node].box_id:
name = nodes[node].name
print(f'name: {name}')
next_id = nodes[node].next
if next_id != []:
next_range = len(next_id)
if next_range < 2:
next_id = next_id[0]
else:
for next_num in range(next_range):
LinkedList(nodes, next_id[next_num])
next_id = []
box_id = next_id
打印
name: Connect 1
name: Connect 2
name: Connect 3
name: Connect 4
name: Connect 5
name: Connect 7
name: Connect 8
name: Connect 6
name: Connect 8
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.