-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLinkedListQueue.py
84 lines (66 loc) · 1.95 KB
/
LinkedListQueue.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from base import QueueBase
class LinkedListQueue(QueueBase):
"""头出尾入"""
class _Node:
def __init__(self, e=None, node_next=None):
self.e = e
self.next = node_next
def __str__(self):
return str(self.e)
def __repr__(self):
return self.__str__()
def __init__(self):
self._head = None
self._tail = None
self._size = 0
def get_size(self):
return self._size
def is_empty(self):
return self._size == 0
def enqueue(self, e):
# 其实也说明了self._head也为空
if not self._tail:
self._tail = self._Node(e)
self._head = self._tail
else:
self._tail.next = self._Node(e)
self._tail = self._tail.next
self._size += 1
def dequeue(self):
if (self.is_empty()):
raise ValueError('Can not dequeue from an empty queue.')
ret = self._head
self._head = self._head.next
ret.next = None
# 特殊情况,如果链表中只有一个元素,此时head和tail应该都为空
if not self._head:
self._tail = None
self._size -= 1
return ret.e
def get_front(self):
if self.is_empty():
raise ValueError('Queue is empty')
return self._head.e
def __str__(self):
curr = self._head
data = []
while curr:
data.append(str(curr.e))
curr = curr.next
return '<chapter_06_LinkedList.linkedlist_queue.LinkedListQueue>: (Tail) ' + \
' <-> '.join(data) + ' (Head)'
def __repr__(self):
return self.__str__()
if __name__ == '__main__':
queue = LinkedListQueue()
queue.enqueue(1)
queue.enqueue(4)
queue.enqueue(100)
queue.enqueue(9)
print(queue)
queue.dequeue()
print(queue)
queue.enqueue('zhe')
print(queue)
queue.dequeue()
print(queue)