之前实现过栈,用过数组,用过链表,但有时候用循环链表能更快的满足我们的程序需要,但是最近要准备考试,就不说了
class Empty(Exception):
pass
class CircularQueue:
class _Node:
def __init__(self, element, next):
self._element = element
self._next = next
def __init__(self):
self._tail = None
self._size = 0
def __len__(self):
return self._size
def is_empty(self):
return self._size == 0
def first(self):
if self.is_empty():
raise Empty("Queue is Empty!")
return self._tail._next._elelment
def enqueue(self, e):
newest = self._Node(e, None)
if self.is_empty():
newest._next = newest
else:
newest._next = self._tail._next
self._tail = newest
self._size += 1
def dequeue(self):
if self.is_empty():
raise Empty("Queue is Empty!")
head = self._tail._next
answer = head._element
if self._size == 1:
self.tail = None
else:
self._tail._next = head._next
self._size -= 1
return answer
def rotate(self):
if self._size > 0:
self._tail = self._tail._next
先这样,如果之后有时间会把这个补上。