-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathQueueImple.py
More file actions
45 lines (41 loc) · 1.07 KB
/
QueueImple.py
File metadata and controls
45 lines (41 loc) · 1.07 KB
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
# Queue class implementation
# Author: Pradeep K. Pant, ppant@cpan.org
# Implement basic operation in Queue
# Stack follows FIFO sequence
# enqueue: adding a element to the queue
# dequeue: Removing an element from queue
# Initialize queue class and set a empty list
class Queue(object):
def __init__(self):
self.items=[]
# Check if list is empty
def isEmpty(self):
return self.items == []
# Insert new element in the queue
def enqueue(self,item):
self.items.insert(0,item)
# Remove element from queue
def dequeue(self):
return self.items.pop()
# Check size of a queue (how may elements are stored)
def size(self):
return len(self.items)
# Test
# Create an object and try basic operation
qObj = Queue()
# Check if list is empty
print (qObj.isEmpty())
# Add an element
qObj.enqueue(1)
# Add another element
qObj.enqueue(2)
# Check again if empty
print (qObj.isEmpty())
# Check size
print (qObj.size())
# show items
print (qObj.items)
# Remove item (First-In-First-Out)
print (qObj.dequeue())
# Check again items
print (qObj.items)