MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / Queue

Class Queue

Hackerrank_problems/Queue using Two Stacks/solution.py:1–27  ·  view source on GitHub ↗

A queue using two stacks (head and tail), the head stack keeps the elements in a queue order, it means ready to dequeue or print the top element. When necessary the last elements in the tail stack are moved to the head stack. The normalize_head method verify if its necessary

Source from the content-addressed store, hash-verified

1class Queue:
2 """
3 A queue using two stacks (head and tail), the head
4 stack keeps the elements in a queue order, it means
5 ready to dequeue or print the top element.
6 When necessary the last elements in the tail stack
7 are moved to the head stack. The normalize_head method
8 verify if its necessary to move elements from tail to head.
9 """
10 head = []
11 tail = []
12
13 def normalize_head(self):
14 if not self.head:
15 while self.tail:
16 self.head.append(self.tail.pop())
17
18 def enqueue(self, element):
19 self.tail.append(element)
20
21 def dequeue(self):
22 self.normalize_head()
23 self.head.pop()
24
25 def print_top(self):
26 self.normalize_head()
27 print(self.head[-1])
28
29if __name__ == '__main__':
30 queue = Queue()

Callers 1

solution.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected