forked from xiao-xiaoming/DataStructure-BeautyOfAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_queue.py
More file actions
59 lines (45 loc) · 1.2 KB
/
Copy pathlinked_queue.py
File metadata and controls
59 lines (45 loc) · 1.2 KB
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
# -*- coding: utf-8 -*-
__author__ = 'xiaoxiaoming'
from typing import Optional
class ListNode:
def __init__(self, data: str, next=None):
self.data = data
self._next = next
class LinkedQueue:
def __init__(self):
self._head: Optional[ListNode] = None
self._tail: Optional[ListNode] = None
def enqueue(self, value: str):
# 入队
new_node = ListNode(value)
if self._tail:
self._tail._next = new_node
else:
self._head = new_node
self._tail = new_node
def dequeue(self) -> Optional[str]:
"""出队"""
if self._head:
value = self._head.data
self._head = self._head._next
if not self._head:
self._tail = None
return value
def __repr__(self) -> str:
values = []
p: ListNode = self._head
while p:
values.append(p.data)
p = p._next
return "->".join(values)
if __name__ == "__main__":
q = LinkedQueue()
for i in range(10):
q.enqueue(str(i))
print(q)
for _ in range(3):
q.dequeue()
print(q)
q.enqueue("7")
q.enqueue("8")
print(q)