-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ4_QueueStack.py
More file actions
70 lines (53 loc) · 1.39 KB
/
Copy pathQ4_QueueStack.py
File metadata and controls
70 lines (53 loc) · 1.39 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Queqe using Two Stack
class Stack:
def __init__(self):
self.list = []
def __len__(self):
if not self.list:
return 0
return len(self.list)
def __str__(self):
if not self.list:
return None
return self.list
def push(self, data):
return self.list.append(data)
def pop(self):
if not self.list:
return None
return self.list.pop()
class QueqeViaStack:
def __init__(self):
self.inStack = Stack()
self.outStack = Stack()
def __str__(self):
if not self.inStack.list:
return None
else:
string = ''
for i in (self.inStack.list):
string += str(i) + ' '
return string
def enqueqe(self, data):
return self.inStack.push(data)
def dequeqe(self):
if not self.inStack.list:
return "Queqe is Empty"
while len(self.inStack):
self.outStack.push(self.inStack.pop())
data = self.outStack.pop()
while len(self.outStack):
self.inStack.push(self.outStack.pop())
return data
myqueqe = QueqeViaStack()
for i in range(1,11):
myqueqe.enqueqe(i)
print(myqueqe)
print(myqueqe.dequeqe())
print(myqueqe)
"""
[output]
1 2 3 4 5 6 7 8 9 10
1
2 3 4 5 6 7 8 9 10
"""