队列链表实现

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
class Node:  
def __init__(self, data, next):
self.data = data
self.next = next

class ListQueue:
def __init__(self):
self.head, self.tail = None, None
def enqueue(self, node):
if self.tail is not None:
self.tail.next = node
self.tail = node

if self.head is None:
self.head = node

def dequeue(self):
data = None
if self.head is not None:
data = self.head.data
self.head = self.head.next
return data

def __repr__(self):
str = ""
head = self.head
while head:
str += "[%r]->"%head.data
head = head.next
str += "None"
return str

if __name__ == "__main__":
l = ListQueue()
for i in range(10):
l.enqueue(Node(i, None))
print(l)

for i in range(5):
print(l.dequeue())
print(l)

for i in range(10):
print(l.enqueue(Node(i, None)))
print(l)