MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / Queue

Class Queue

data_structures/queue/queue_on_pseudo_stack.py:2–50  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1"""Queue represented by a pseudo stack (represented by a list with pop and append)"""
2class Queue():
3 def __init__(self):
4 self.stack = []
5 self.length = 0
6
7 def __str__(self):
8 printed = '<' + str(self.stack)[1:-1] + '>'
9 return printed
10
11 """Enqueues {@code item}
12 @param item
13 item to enqueue"""
14 def put(self, item):
15 self.stack.append(item)
16 self.length = self.length + 1
17
18 """Dequeues {@code item}
19 @requirement: |self.length| > 0
20 @return dequeued
21 item that was dequeued"""
22 def get(self):
23 self.rotate(1)
24 dequeued = self.stack[self.length-1]
25 self.stack = self.stack[:-1]
26 self.rotate(self.length-1)
27 self.length = self.length -1
28 return dequeued
29
30 """Rotates the queue {@code rotation} times
31 @param rotation
32 number of times to rotate queue"""
33 def rotate(self, rotation):
34 for i in range(rotation):
35 temp = self.stack[0]
36 self.stack = self.stack[1:]
37 self.put(temp)
38 self.length = self.length - 1
39
40 """Reports item at the front of self
41 @return item at front of self.stack"""
42 def front(self):
43 front = self.get()
44 self.put(front)
45 self.rotate(self.length-1)
46 return front
47
48 """Returns the length of this.stack"""
49 def size(self):
50 return self.length

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected