-
Notifications
You must be signed in to change notification settings - Fork 62
/
linked_queue.py
68 lines (50 loc) · 1.35 KB
/
linked_queue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 使用链表实现一个栈
class ListNode:
def __init__(self, val: int):
self.val = val
self.next = None
class LinkedQueue:
def __init__(self, capacity: int):
self.head = None
self.tail = None
self.size = 0
self.capacity = capacity
# 入队列
def enqueue(self, val: int):
if self.isFull():
raise RuntimeError('the queue is full')
node = ListNode(val)
if self.tail:
self.tail.next = node
if not self.head:
self.head = node
self.tail = node
self.size += 1
# 出队列
def dequeue(self):
if self.isEmpty():
raise RuntimeError('the queue is empty')
res = self.head.val
self.head = self.head.next
self.size -= 1
return res
# 队列是否已满
def isFull(self) -> bool:
return self.size >= self.capacity
# 队列是否已空
def isEmpty(self) -> bool:
return self.size <= 0
if __name__ == '__main__':
queue = LinkedQueue(10)
queue.enqueue(4)
queue.enqueue(3)
queue.enqueue(1)
queue.enqueue(5)
print(queue.dequeue())
print(queue.dequeue())
print(queue.dequeue())
print(queue.dequeue())
queue.enqueue(10)
print(queue.dequeue())
print(queue.isFull())
print(queue.isEmpty())