2017-10-01 14 views
0
와 우선 순위 큐를 구현할 때
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 . . . 
+0

당신이 당신의 질문에 대한 역 추적을 추가 할 수 있습니까? – Vinny

+0

미안하지만, 나쁘다. 고마워. 방금 추가 했어. –

답변

0

귀하의 while 루프 작동합니다. NoneType에 도달 할 때까지 last = last.next을 계속 전달합니다. last에서 last.next으로 진행하기 전에 거기에 노드가 있는지 확인하십시오. 나는 코드의이 부분 수정했습니다

elif (self.head.priority == 'H' and newNode.priority == 'H'): 
     last = self.head 
     print(self.head.priority) 
     print(last.priority) 
     while last.priority == 'H' and last.next: # <-- check last.next 
                 exists before pointing to it 
      last = last.next 
     if last.next and last.next.next: # <-- same thing here 
      newNode.next = last.next.next 
     last.next = newNode 

을이 출력입니다 :

>>> main() 
H 
H 
+0

고마워요. 제안한대로 변경했지만 특정 입력 집합에 대한 런타임 오류가 발생했습니다. 위의 코드를 업데이트 했습니까? 도와 줘서 고마워. –

+0

사실 나는 그것을 가지고 있다고 생각합니다. 도와 주셔서 정말 감사합니다! –

+0

@SifanXu 도와 주셔서 감사합니다. 당신이 내 대답을 찾으면 도움이 발견, 그것을 시도, 덕분에 – Vinny