Python队列链表

2024-06-30 15:50:37 发布

您现在位置: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`

这是我的单元测试用例

^{pr2}$

这就是我不断收到我的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'

我不明白什么?在


Tags: theinselfnodedatareturnifdef
1条回答
网友
1楼 · 发布于 2024-06-30 15:50:37

正如评论中已经有人指出的那样,您发布的回溯表明set_data是在enqueue方法中调用的,而这并没有反映在只调用set_next的代码中。不过,我假设您在set_next中也会遇到类似的错误。在

QueueLinked中,使用值None初始化{}。在enqueue方法中,用self.rear的值初始化变量oldrear,在调用enqueue时,该值仍然是None。此时oldrear和{}都是None。然后尝试调用set_next(仍然==None)并得到一个错误,告诉您{}没有set_next方法。在

我还没有检查整个代码,所以我不确定这是否正确,但是我假设您可以初始化self.rear = Node(),它就可以工作了。在

相关问题 更多 >