-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueListBased.py
More file actions
78 lines (59 loc) · 1.46 KB
/
QueueListBased.py
File metadata and controls
78 lines (59 loc) · 1.46 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class QueueListBased:
def __init__(self, data=None):
self.__item = []
self.__size = 0
if data:
self.__item.insert(0, data)
self.__size += 1
# Add to a list
def enque(self, data):
self.__item.append(data)
self.__size += 1
# Remove from a list
def deque(self):
'''
We can say the deque operation is highly inefficient. the method has to shift all the elements by one
after ever opeation
'''
if self.__item:
self.__size -= 1
return self.__item.pop(0)
else:
raise IndexError('Queue is Empty')
# check the number of element in Queue
def get_size(self):
return self.__size
def iter(self):
for item in self.__item:
yield item
def search(self, value):
for item in self.__item:
if value == item:
return True
return False
# Below code is to test above written functionalites
import math
import time
from functools import wraps
def record_time(func):
@wraps(func)
def wrapper(*args, **kargs):
start = time.time()
# res = yield from func(*args, **kargs)
res = func(*args, **kargs)
end = time.time()
print("**** The taken is {} *****".format(end- start))
return res
return wrapper
@record_time
def test():
print('creatin the Queue with first elemetn as 1 ')
my_queue = QueueListBased(1)
print('adding million items to list')
for i in range(2, 10**6):
my_queue.enque(i)
print('Deque an element from my Queue')
print(my_queue.deque())
print('Printe the size of the Queue')
print(my_queue.get_size())
test()