-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.py
More file actions
56 lines (45 loc) · 1.31 KB
/
queue.py
File metadata and controls
56 lines (45 loc) · 1.31 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
# Python3 program to implement Queue
# using two stacks
class Queue:
def __init__(self):
self.s1 = []
self.s2 = []
# Add an item to the queue
def add(self, x):
self.s1.append(x)
def __front(self, pop=True):
# if both stacks are empty
if len(self.s1) == 0 and len(self.s2) == 0:
return -1
# if s2 is empty and s1 has elements
elif len(self.s2) == 0 and len(self.s1) > 0:
while len(self.s1):
temp = self.s1.pop()
self.s2.append(temp)
if pop:
return self.s2.pop()
return self.s2[-1]
else:
if pop:
return self.s2.pop()
return self.s2[-1]
# Remove an item from the queue
def remove(self):
return self.__front(pop=True)
# Returns the value of the top("front") item of the queue
def peek(self):
return self.__front(pop=False)
# Returns the number of items in the queue
def length(self):
return len(self.s1) + len(self.s2)
# Returns whether queue has items or not
def is_empty(self):
return self.length() == 0
if __name__ == '__main__':
q = Queue()
q.add(1)
q.add(2)
q.add(3)
print(q.remove())
print(q.remove())
print(q.remove())