Question

In: Computer Science

Implement the stack class (called Stack2) using queue, meaning that the only operations that can be...

Implement the stack class (called Stack2) using queue, meaning that the only operations that can be used are the ones defined in the Queue class.

class Queue:
def __init__(self):
self.items = []

def isEmpty(self):
return self.items == []

def enqueue(self, item):
self.items.insert(0, item)

def dequeue(self):
return self.items.pop()

def size(self):
return len(self.items)

The codes to fill:

"""
1. Stack2 class
Implement stack data structure using queue
"""
class Stack2:
def __init__(self):
# Write your definition for __init__ here

def isEmpty(self):
# Write your definition for isEmpty here

def push(self, item):
# Write your definition for push here

def pop(self):
# Write your definition for pop here

def peek(self):
# Write your definition for peek here

def size(self):
# Write your definition for size here

Solutions

Expert Solution

A stack can be implemented using two queues. One main queue will store the elements and other helper queue will help to maintain the ordering. Here, there are two choices: Either to make push costly or pop costly. In this particular program, push is costly while pop is just O(1). Program is given at the end.

Working of given Queue:

The Queue class given in the question inserts elements at the beginning of the queue while deletes elements from the end. Since pop should be O(1), the queue should be maintained in such a way that last inserted element is always at the end. By maintaining this property, pop simply requires to call the dequeue of the main queue.

Different methods implementation:

1. __init__(self) : Since it is a constructor, two queues are initialized: q1 and q2. q1 is the main queue and q2 is the helper queue.

2. push(self, item): This is the main function as this is the function in which the above stated property will be maintained. Suppose that q1 contains the elements in the required order before inserting item. If item is inserted into q1 directly, then item will be at the front of q1 while it must be at the end for pop to work. To resolve this issue, item is first inserted into empty queue q2. Then one by one ,elements from q1 are deleted and inserted into q2. This way the relative ordering is maintained but the elements are now in queue q2 while it is required that they must be in q1. To resolve this, q1 and q2 are swapped.

3. pop(self): Check if q1 is not empty. Then dequeue an element from q1.

4. peek(self): This function returns the last inserted element but does not remove it. Since the Queue class does not contain any method to get an element from some location, (and only Queue operations can be used) there is a roundabout. First dequeue an element from q1. Store it in x. Push it again in the Stack2. Return x. This way the contents of stack2 do not change while the last inserted element(x) can be returnd.

5. size(self): return the size of q1.

Example:

Suppose 1,2 and 3 are inserted in stack2:

Push 1: q1 = [1], q2 = []

Push 2: q1 = [1,2], q2 = [] [2 is inserted into q2 first, then element of q1 (1) is inserted into q2. After that q1 and q2 are swapped]

Push 3: q1 = [1,2,3], q2 = [] [Similar to the above]

Pop: q1 = [1,2] , q2=[] [Dequeue from q1]

Program in Python:

class Queue:
    def __init__(self):
        self.items = []

    def isEmpty(self):
        return self.items == []

    def enqueue(self, item):
        self.items.insert(0, item)

    def dequeue(self):
        return self.items.pop()

    def size(self):
        return len(self.items)

"""
1. Stack2 class
Implement stack data structure using queue
"""
# A stack can be implemented using two queues
class Stack2:
    def __init__(self):
        self.q1 = Queue()
        self.q2 = Queue()

    def isEmpty(self):
        return self.q1.isEmpty()

    def push(self, item):
        self.q2.enqueue(item)
        while not self.q1.isEmpty():
            self.q2.enqueue(self.q1.dequeue())
        self.q1, self.q2 = self.q2, self.q1

    def pop(self):
        if self.q1.isEmpty():
            return
        return self.q1.dequeue()

    def peek(self):
        if self.q1.isEmpty():
            return
        x = self.q1.dequeue()
        # Same as pushing x
        # Alternatively can us 'self.push(x)'
        self.q2.enqueue(x)
        while not self.q1.isEmpty():
            self.q2.enqueue(self.q1.dequeue())
        self.q1, self.q2 = self.q2, self.q1
        return x

    def size(self):
        return this.q1.size()

#Testing
st = Stack2()
st.push(0)
st.push(1)
st.push(2)
st.push(3)
print(st.peek())
print(st.pop())
print(st.pop())
print(st.pop())
print(st.pop())

Output:

3
3
2
1
0

Related Solutions

Implement a stack using a single queue. In particular, you are given a queue Q that...
Implement a stack using a single queue. In particular, you are given a queue Q that provides the method Q.size() to return its size at any point and the standard methods of queues (i.e, Q.enqueue(x) and Q.dequeue()). The requirement is to use such methods of Q to implement two methods S.push(x) and S.pop() for a stack S. What are the running times of your methods? Kindly answer using python programming
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
In C++, implement a class called setOper that provides several simple set operations. The class only...
In C++, implement a class called setOper that provides several simple set operations. The class only needs to deal with sets that are closed intervals specified by two real numbers; for example, the pair (2.5, 4.5) represent the interval [2.5, 4.5]. The following operations should be supported: - Check if the value x belongs to the given interval. - Check if the value x belongs to the intersection of two intervals. - Check if the value x belongs to the...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For simplicity the data type to be stored in the stack is String but you can also use generic type. 2. Test your class implementation by calling push() and pop(). If the stack is empty (size equals zero) pop() should return null and print that “stack is empty”. If stack is full (size equals max) push() returns false and prints that “stack is full”. This...
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the...
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the java and give the explanation
Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of...
Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of the method push, pop, top, isEmpty, and size. You should not use any extra data structure. Related codes: public interface Stack<E> { int size( ); boolean isEmpty( ); void push(E e); E top( ); E pop( ); } public class LinkedStack<E> implements Stack<E> { private SinglyLinkedList<E> list = new SinglyLinkedList<>( );    public LinkedStack( ) { }    public int size( ) { return...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
Are you able to implement a stack using just one queue? If yes, please provide the...
Are you able to implement a stack using just one queue? If yes, please provide the pop(only) algorithm to simulate the stack operations with just one queue. If yes, please provide the pop(only) algorithm to simulate the stack operations with just one queue. If no, explain.
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT