the art of
Algorithm
Notes on Analysis and Design



Queue Using Two Stacks
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
87
88
89
90
91
92
93
94
95
class Node:
    """
    Node of Linked List
    """
    def __init__(self, item=None, link=None):
        self.data = item
        self.next = link


class Stack:
    """
    Stack Implementation using Linked List
    """
    def __init__(self):
        self.head = None

    def push(self, item):
        new_node = Node(item, self.head)
        self.head = new_node

    def pop(self):
        if not self.isEmpty():
            popped_node = self.head
            if self.head.next:
                self.head = self.head.next
            else:
                self.head = None
            return popped_node.data
        else:
            raise IndexError("Trying to remove from empty stack!")

    def isEmpty(self):
        """
        Returns true if stack is empty, otherwise false
        """
        return self.head == None

    def size(self):
        """
        Returns size of stack
        """
        size = 0
        temp = self.head
        while temp:
            size += 1
            temp = temp.next
        return size

    def __str__(self):
        temp = self.head
        res = ''
        while temp:
            res += ' ' + str(temp.data)
            temp = temp.next
        return res


class Queue:
    """
    Queue Implementation using Stack
    """
    def __init__(self):
        self.stack1 = Stack()
        self.stack2 = Stack()

    def enqueue(self, item):
        while not self.stack1.isEmpty():
            self.stack2.push(self.stack1.pop())

        self.stack1.push(item)

        while not self.stack2.isEmpty():
            self.stack1.push(self.stack2.pop())

    def dequeue(self):
        removed_element = self.stack1.pop()
        return removed_element

    def __str__(self):
        return str(self.stack1)

mstack = Stack()
mstack.push(9)
mstack.push(10)
print(mstack)
mstack.pop()
print(mstack)


mqueue = Queue()
mqueue.enqueue(9)
mqueue.enqueue(10)
print(mqueue)
mqueue.dequeue()
print(mqueue)