-
Notifications
You must be signed in to change notification settings - Fork 591
/
P39_Queue.py
51 lines (42 loc) · 1.44 KB
/
P39_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
# Author: OMKAR PATHAK
# This program illustrates an example of Queue implementation in Python
# Stack Operations: enqueue(), dequeue(), isFull(), isEmpty, peek()
class Queue(object):
def __init__(self, size):
self.queue = []
self.size = size
def __str__(self):
myString = ' '.join(str(i) for i in self.queue)
return myString
def enqueue(self, item):
'''This function adds an item to the rear end of the queue '''
if(self.isFull() != True):
self.queue.insert(0, item)
else:
print('Queue is Full!')
def dequeue(self):
''' This function removes an item from the front end of the queue '''
if(self.isEmpty() != True):
return self.queue.pop()
else:
print('Queue is Empty!')
def isEmpty(self):
''' This function checks if the queue is empty '''
return self.queue == []
def isFull(self):
''' This function checks if the queue is full '''
return len(self.queue) == self.size
def peek(self):
''' This function helps to see the first element at the fron end of the queue '''
if(self.isEmpty() != True):
return self.queue[-1]
else:
print('Queue is Empty!')
if __name__ == '__main__':
myQueue = Queue(10)
myQueue.enqueue(1)
myQueue.enqueue(2)
myQueue.enqueue(3)
print(myQueue)
myQueue.dequeue()
print(myQueue)