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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 | class Node:
def __init__(self, item=None, link=None):
self.data = item
self.next = link
class Queue:
def __init__(self):
self.front = None
self.rear = None
def enqueue(self, item):
new_node = Node(item)
if self.front == None:
self.front = new_node
else:
self.rear.next = new_node
self.rear = new_node
def dequeue(self):
if not self.isEmpty():
removed_element = self.front.data
if self.front.next:
self.front = self.front.next
else:
self.front = None
self.rear = None
return removed_element
else:
raise IndexError("Trying to remove from empty queue!")
def isEmpty(self):
return self.front == None
def size(self):
temp = self.front
count = 0
while temp:
count += 1
temp = temp.next
return count
def __str__(self):
temp = self.front
res = ''
while temp:
res += ' ' + str(temp.data)
temp = temp.next
return res
class Stack:
def __init__(self):
self.queue1 = Queue()
self.queue2 = Queue()
def push(self, item):
self.queue1.enqueue(item)
def pop(self):
queue_size = self.queue1.size()
while queue_size != 1 and queue_size > 0:
self.queue2.enqueue(self.queue1.dequeue())
queue_size -= 1
removed_element = self.queue1.dequeue()
self.queue1, self.queue2 = self.queue2, self.queue1
return removed_element
def __str__(self):
return str(self.queue1)
mqueue = Queue()
mqueue.enqueue(10)
mqueue.enqueue(9)
mqueue.enqueue(8)
print(mqueue)
mqueue.dequeue()
print(mqueue)
mstack = Stack()
mstack.push(10)
mstack.push(9)
mstack.push(8)
print(mstack)
mstack.pop()
print(mstack)
|