class Node:
def __init__(self, data=None, priority='L', next=None):
self.data = data
self.priority = priority
self.next = next
def __str__(self):
return str(self.data)
class P_Queue:
def __init__(self, head=None):
self.head = head
self.length = 0
def enqueue(self, node):
newNode = Node(node.data, node.priority)
if (self.head == None):
self.head = newNode
elif (self.head and self.head.priority == 'L' and newNode.priority == 'H'):
newNode.next = self.head
self.head = newNode
elif (self.head and self.head.priority == 'H' and newNode.priority == 'H'):
last = self.head
while (last.next and last.next.priority == 'H'):
last = last.next
if (last.next and last.next.next):
newNode.next = last.next.next
last.next = newNode
else:
last = self.head
while last.next:
last = last.next
last.next = newNode
self.length += 1
def dequeue(self):
node = self.head
print("next head: ")
print(self.head.next)
self.head = self.head.next
self.length = self.length - 1
return node
def is_empty(self):
return self.length==0
def main():
node0 = Node(0, 'L')
node1 = Node(1, 'H')
node2 = Node(2, 'H')
queue = P_Queue()
queue.enqueue(node0)
queue.enqueue(node1)
queue.enqueue(node2)
print(queue.dequeue())
print(queue.dequeue())
print(queue.dequeue())
main()
문제는 대기열에 while 문(), I "는 오류에 표시된 코드의 마지막 줄을 발생
은 '특수 철골 구조'개체가 어떤 속성 속성이 없습니다 . "하지만 큐 (node1)에 대해서만파이썬 : NoneType AttributeError가 링크 노드
그러나 node0 = Node (0, 'H')에 대한 print 문 (출력 : H)에 따르면이 속성에 대해 'H' (우선 순위)이며 'None'값을 포함하지 않으므로 나에게 정신이 나간다.
도와주세요. 훌륭한 사람이라면 초보자를위한 우선 순위 대기열을 구현하는 방법을 배우기에 좋은 자료가 있습니다. 고마워, 내가 여기서 죽어 가고있어. 아래
역 추적 :
next head: 2
1
next head: None
2
next head:
Traceback (most recent call last):
File "assignment1_3 queues.py", line 62, in <module>
main()
File "assignment1_3 queues.py", line 60, in main
print(queue.dequeue())
File "assignment1_3 queues.py", line 39, in dequeue
print(self.head.next)
AttributeError: 'NoneType' object has no attribute 'next'
------------------
(program exited with code: 1)
Press any key to continue . . .
당신이 당신의 질문에 대한 역 추적을 추가 할 수 있습니까? – Vinny
미안하지만, 나쁘다. 고마워. 방금 추가 했어. –