-
Notifications
You must be signed in to change notification settings - Fork 623
Expand file tree
/
Copy pathqueue.py
More file actions
76 lines (57 loc) · 2.25 KB
/
queue.py
File metadata and controls
76 lines (57 loc) · 2.25 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
class Queue:
"""
Queue is an abstract data structure, somewhat similar to Stacks.
Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue)
and the other is used to remove data (dequeue).
Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.
"""
class __Node: # new addition part
""" used for double linked list logic of queue"""
""" should not available outside the class"""
def __init__(self,element,next = None,prev = None):
self.ele = element
self.next = next
def __init__(self): # modified part
self.head = None # points to last pushed element (enqueue part)
self.length = 0 # number of elements in it
self.Tail = None # points to first pushed element (dequeue part)
def is_empty(self): # new addition part
"""check is queue object is empty or not
return boolean
"""
return self.length<=0
def put(self, item): # modified part
"""add item to queue
Args:
item (any): item to be pushed
"""
temp = self.__Node(item) # create new node
if self.is_empty():
# initialize the doubly linked list
self.head = temp
self.Tail = temp
self.length += 1
return
# already initialized so just add the item
self.head.next = temp # point to next element
self.head = self.head.next # move pointer to next element which is last pushed element
self.length += 1
return
def get(self): # modified part
""" pop the tail element and move the pointer one step ahead
"""
if self.is_empty():
return
self.length -= 1
temp = self.Tail
nex = self.Tail.next
self.Tail.next = None
self.Tail = nex
return temp.ele
def rotate(self, rotation):
for i in range(rotation):
self.put(self.get())
def size(self):
return self.length
def __len__(self): # new addition part
return self.length