Skip to content

Commit d79105d

Browse files
committed
Added queue on stack
1 parent 13dd4a1 commit d79105d

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""Queue represented by a pseudo stack (represented by a list with pop and append)"""
2+
classQueue():
3+
def__init__(self):
4+
self.stack= []
5+
self.length=0
6+
7+
def__str__(self):
8+
printed='<'+str(self.stack)[1:-1] +'>'
9+
returnprinted
10+
11+
"""Enqueues{@code item}
12+
@param item
13+
item to enqueue"""
14+
defput(self, item):
15+
self.stack.append(item)
16+
self.length=self.length+1
17+
18+
19+
"""Dequeues{@code item}
20+
@requirement: |self.length| > 0
21+
@return dequeued
22+
item that was dequeued"""
23+
defget(self):
24+
self.rotate(1)
25+
dequeued=self.stack[self.length-1]
26+
self.stack=self.stack[:-1]
27+
self.rotate(self.length-1)
28+
self.length=self.length-1
29+
returndequeued
30+
31+
"""Rotates the queue{@code rotation} times
32+
@param rotation
33+
number of times to rotate queue"""
34+
defrotate(self, rotation):
35+
foriinrange(rotation):
36+
temp=self.stack[0]
37+
self.stack=self.stack[1:]
38+
self.put(temp)
39+
self.length=self.length-1
40+
41+
"""Reports item at the front of self
42+
@return item at front of self.stack"""
43+
deffront(self):
44+
front=self.get()
45+
self.put(front)
46+
self.rotate(self.length-1)
47+
returnfront
48+
49+
"""Returns the length of this.stack"""
50+
defsize(self):
51+
returnself.length

‎data_structures/Queue/__init__.py‎

Whitespace-only changes.

‎data_structures/__init__.py‎

Whitespace-only changes.

0 commit comments

Comments
(0)