File tree 4 files changed +51
-0
lines changed
4 files changed +51
-0
lines changed File renamed without changes.
Original file line number Diff line number Diff line change
1
+ """Queue represented by a pseudo stack (represented by a list with pop and append)"""
2
+ class 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
+
19
+ """Dequeues {@code item}
20
+ @requirement: |self.length| > 0
21
+ @return dequeued
22
+ item that was dequeued"""
23
+ def get (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
+ return dequeued
30
+
31
+ """Rotates the queue {@code rotation} times
32
+ @param rotation
33
+ number of times to rotate queue"""
34
+ def rotate (self , rotation ):
35
+ for i in range (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
+ def front (self ):
44
+ front = self .get ()
45
+ self .put (front )
46
+ self .rotate (self .length - 1 )
47
+ return front
48
+
49
+ """Returns the length of this.stack"""
50
+ def size (self ):
51
+ return self .length
You can’t perform that action at this time.
0 commit comments