-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueNodeBased.py
More file actions
83 lines (66 loc) · 1.57 KB
/
Copy pathQueueNodeBased.py
File metadata and controls
83 lines (66 loc) · 1.57 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
79
80
81
82
class Node():
def __init__(self , data=None, prev=None, next_ref=None):
self.data = data
self.prev = prev
self.next = next_ref
class QueueNodeBased:
def __init__(self, data=None):
self.head = None
self.tail = None
self.count = 0
if data:
node = Node(data)
self.head = node
self.tail = node
self.count += 1
def enque(self, value):
node = Node(value, self.tail, None)
if self.head == None:
self.head = node
self.tail = node
else:
self.tail.next = node
self.tail = node
self.count += 1
def deque(self):
if self.head == None:
raise IndexError('Queue is Empty')
elif self.count == 1:
value = self.head.data
self.head = None
self.tail = None
self.count -= 1
return value
else:
value = self.head.data
self.head = self.head.next
self.count -= 1
return value
def get_size(self):
return self.count
# 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 = QueueNodeBased(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()