-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.py
61 lines (58 loc) · 1.38 KB
/
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
#encoding=utf-8
from linklist import *
'''
实现一个链式队列
'''
class Queue(object):
def __init__(self):
self.front=None
self.tail=None
self.count=0
def enqueue(self,data):
"入队 实际是上就是链表的尾插法"
if self.count==0:
self.front=LinkList(data)
self.tail=self.front
else:
temp=LinkList(data)
self.tail.setNext(temp)
self.tail=temp
self.count+=1
def dequeue(self):
if self.count==0:
return None
else:
temp=self.front.getData()
self.front=self.front.getNext()
self.count -=1
return temp
def __len__(self):
return self.count
def peek(self):
'''
return the front without removing it
:return:
'''
if self.count==0:
return None
else:
return self.front.getData()
if __name__ == '__main__':
queue=Queue()
print queue
print queue.front
print queue.tail
queue.enqueue("ee")
print queue.front
print queue.tail
queue.enqueue(23)
queue.enqueue("wang")
print queue.tail
print(len(queue))
print queue.dequeue()
print(len(queue))
print queue.tail
print queue.dequeue()
print queue.dequeue()
print queue.dequeue()
print(len(queue))