-
Notifications
You must be signed in to change notification settings - Fork 70
/
example_007_queue.py
64 lines (46 loc) · 1.43 KB
/
example_007_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
62
63
64
'''Example of a Queue implementation.'''
class Queue():
'''Queue implements the FIFO principle.'''
def __init__ (self):
self.queue = []
def enqueue(self, item):
'''Takes in an item and adds it to the end of the queue.'''
self.queue.append(item)
def dequeue(self):
'''Removes an item from the end of the queue.'''
if not self.is_empty():
return self.queue.pop(0)
return None
def is_empty(self):
'''Checks if the queue is empty.'''
return len (self.queue) == 0
def size(self):
'''Returns the number of elements in the queue.'''
return len(self.queue)
def __str__(self):
'''Returns a string representation of the queue.'''
return str(self.queue)
###############################
# #
# Example run of a Queue #
# #
###############################
def main():
'''A main function to demonstrate the queue functions.'''
my_queue = Queue()
# Enqueue 10
my_queue.enqueue(10)
print(my_queue)
# Enqueue 18
my_queue.enqueue(18)
print(my_queue)
# Enqueue 1024
my_queue.enqueue(1024)
print(my_queue)
# dequeue()
print('Dequeue ', my_queue.dequeue())
print('Dequeue ', my_queue.dequeue())
print('Dequeue ', my_queue.dequeue())
print('Dequeue ', my_queue.dequeue())
if __name__ == '__main__':
main()