当队列中没有元素时,head和tail都是-1,
当进如一个元素时,如下图,此时tail加1,也就是inQueue函数中self.tail += 1
class Queue(object):
"""docstring for Queue"""
def __init__(self,size):
self.queue = []
self.size = size
self.head = -1
self.tail = -1
def inQueue(self,data):
if self.is_full():
print("Queue is full")
else:
self.queue.append(data)
self.tail += 1
def outQueue(self,data):
if self.is_empty():
print("Queue is empty")
else:
self.head += 1
def is_empty(self):
if self.head == self.tail:
return True
else:
return False
def is_full(self):
if self.tail - self.head + 1 == self.size:
return True
else:
return False
q = Queue(5)
q.inQueue(1)
q.outQueue(1)
print(q.is_empty())