所以我的老师让我们使用堆栈链表为我的数据结构类实现一个队列。我已经提出了以下代码,我似乎不理解python在运行单元测试时给出的错误......
这是我的代码`
devDependencies
这是我的单元测试用例
node_modules
这是我继续接收我的Pycharm的错误..
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`
我不明白什么?
答案 0 :(得分:0)
正如评论中已有人指出的那样,您发布的追溯表示set_data
方法中调用了enqueue
,而您的代码只会调用set_next
。但是我假设您在set_next
时遇到了类似的错误。
在QueueLinked
中,您使用值self.rear
初始化None
。在enqueue
方法中,您使用oldrear
的值初始化变量self.rear
,该值enqueue
在调用None
时仍为oldrear
。此时self.rear
和None
都是set_next
。您试图在oldrear
上调用None
(仍然== None
)并收到错误消息,告诉您set_next
没有self.rear = Node()
方法
我没有查看整个代码,因此我不确定这是否正确,但我认为您可以初始化async function asyncFunctionINeedToCall() {
await childAsyncFunction()
}
asyncFunctionINeedToCall()
并且它可以正常工作。