How to Implement queue in Python
This example describes how to implement a queue in Python. Share it with you for your reference. The specific implementation method is as follows:
?
|
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 |
#! /Usr/bin/env python
Queue = []
Def enQ ():
Queue. append (raw_input ('enter new string: '). strip ())
# Call the pop () function of the list. pop (0) is the first element of the list.
Def deQ ():
If len (queue) = 0:
Print 'cannot pop from an empty queue! '
Else:
Print 'removed [', queue. pop (0),']'
Def viewQ ():
Print queue
CMDs = {'E': enQ, 'D': deQ, 'V': viewQ}
Def showmenu ():
Pr = """
(E) nqueue
(D) equeue
(V) iew
(Q) uit
Enter choice :"""
While True:
While True:
Try:
Choice = raw_input (pr). strip () [0]. lower ()
Handle T (EOFError, KeyboardInterrupt, IndexError ):
Choice = 'Q'
Print '\ nYou picked: [% s]' % choice
If choice not in 'devq ':
Print 'invalid option, try again'
Else:
Break
If choice = 'q ':
Break
CMDs [choice] ()
If _ name _ = '_ main __':
Showmenu ()
|
I hope this article will help you with Python programming.