2017-10-06 5 views
0

私の先生は私のデータ構造クラスのスタックリンクリストを使ってキューを実装しています。ここでは...Pythonのキューにリンクされたリスト

を私は次のコードが出ている、と私は私が私のユニットテストを実行したときのpythonが私を与えることを、エラーを理解していないように見える私のコードはここで `

class QueueLinked: 

    def __init__(self,capacity): 
     self.capacity = capacity # a capacity 
     self.num_items = 0 
     self.front = None 
     self.rear = None 

    def is_empty(self):  # This function will retrun a bool if the number of items is = to 0 
     return self.num_items == 0 

    def is_full(self): 
     return self.num_items == self.capacity 

    def enqueue(self, item): 
     if self.num_items == self.capacity: 
      raise IndexError('Can\'t enqueue into full queue.') 
     else: 
      self.num_items +=1 
      temp = Node() # this creates a temporary node 
      oldrear = self.rear 
      self.rear = temp 
      oldrear.set_next(self.rear) 


    def dequeue(self): 
     if self.num_items == 0: # this will through an exception because if there are no items we cant pop 
      raise IndexError('Can\'t dequeue from empty queue.') 
     else: 
      self.num_items -=1 
      oldfront = self.front 
      self.front = self.front.get_next() 
      return oldfront.get_data() 


    def num_in_queue(self): 
     return self.num_items 


class Node: 

    def __init__(self): 
     self.next = None # this initializes a node with next pointing to none 

    def set_data(self, data): # this passes the parameter data to the data portion of the node 
     self.data = data # this constructs that data portion of a node everytime we create a node 

    def get_data(self): # get data from the node that was previous newwest 
     return self.data # returns the data from that node 

    def set_next(self, newNext): # this will set a new next to point as in after the head 
     self.next = newNext # this constructs the next portion of the 2 part portion from the node 

    def get_next(self): # this will retrieve the next value from the node 
     return self.next` 

である私のユニットでありますテストケース

import unittest 

from queues import * 


class TestCase(unittest.TestCase): 


    # testing an empty Array 

    def test_if_empty(self): # we will test if the array is empty using is_empty 
     q = QueueLinked(3) # [none,none,none] 
     self.assertTrue(q.is_empty()) # Should be True 

    def test_if_full(self): 
     q = QueueLinked(3) 
     q.enqueue(4) 
     q.enqueue(5) 
     q.enqueue(8) 
     self.assertTrue(q.is_empty()) 

if (__name__ == '__main__'): 

、これは私が私のPycharmを受信し続けるエラー..です

Ran 2 tests in 0.000s 

FAILED (errors=1) 
Launching unittests with arguments python -m unittest test_queues.TestCase in C:\Users\M\Documents\CSC 202\Labs\Lab3 
Error 
Traceback (most recent call last): 
    File "C:\Users\M\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 59, in testPartExecutor 
    yield 
    File "C:\Users\M\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 605, in run 
    testMethod() 
    File "C:\Users\M\Documents\CSC 202\Labs\Lab3\test_queues.py", line 17, in test_if_full 
    q.enqueue(4) 
    File "C:\Users\M\Documents\CSC 202\Labs\Lab3\queues.py", line 63, in enqueue 
    oldrear.set_data(self.rear) 
AttributeError: 'NoneType' object has no attribute 'set_data' 

私は何を理解していないのですか?

+0

最初のパス 'self.rear == None'なので' oldrear'を設定すると 'None.set_data'を呼び出そうとしています – jmunsch

+1

正しいコードを入れましたか? 'Queue()。enqueue()'に 'oldrear.set_data()'がありません。 –

答えて

0

あなたが投稿したトレースバックにはenqueueメソッドでset_dataが呼び出されていますが、これはset_nextというコードだけに反映されていません。しかし、私はあなたがset_nextと同様のエラーを取得すると仮定しています。

QueueLinkedself.rearを値Noneで初期化します。 enqueueメソッドでは、変数oldrearをの値で初期化します。時刻はenqueueで、まだNoneです。この時点で、oldrearself.rearの両方がNoneです。 (まだ== None)のset_nextに電話し、Noneにはset_nextの方法がないことを伝えるエラーが表示されます。

私はコード全体を調べていないので、これが正しいかどうかはわかりませんが、self.rear = Node()を初期化してうまくいくと思われます。

関連する問題