Question

In: Computer Science

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 list.size( ); }

   public boolean isEmpty( ) { return list.isEmpty( ); }

   public void push(E element) { list.addFirst(element);}

   public E top( ) { return list.first( ); }

   public E pop( ) { return list.removeFirst( ); }

   }

---------------------------------------

public interface Queue<E> {

int size( );

boolean isEmpty( );

void enqueue(E e);

E first( );

E dequeue( );

}

public class LinkedQueue<E> implements Queue<E> {

private SinglyLinkedList<E> list = new SinglyLinkedList<>( ); // an empty list

public LinkedQueue( ) { } // new queue relies on the initially empty list

public int size( ) { return list.size( ); }

public boolean isEmpty( ) { return list.isEmpty( ); }

public void enqueue(E element) { list.addLast(element); }

public E first( ) { return list.first( ); }

public E dequeue( ) { return list.removeFirst( ); }

}

Solutions

Expert Solution

Based on the format given, I shall give my own code similar to it.

There are two ways to implement a stack using queue, in one approach, the time complexity of push function increases while in the other, pop's time complexity rises.

1.

In this approach, if we have to push an element, we push it to q2 and then pop all the elements of q1 and push into q2 and then swap names of q1 and q2 which takes order of O(n). top, isempty and size are all of order O(1)

For pop(), we simply pop the element from q1 and return it in order of O(1)

import java.util.*; 

class Main { 

        static class Stack { 
                // Two inbuilt queues 
                static Queue<Integer> q1 = new LinkedList<Integer>(); 
                static Queue<Integer> q2 = new LinkedList<Integer>(); 

                // To maintain current number of 
                // elements 
                static int curr_size; 

                Stack() 
                { 
                        curr_size = 0; 
                } 

                static void push(int x) 
                { 
                        curr_size++; 

                        // Push x first in empty q2 
                        q2.add(x); 

                        // Push all the remaining 
                        // elements in q1 to q2. 
                        while (!q1.isEmpty()) { 
                                q2.add(q1.peek()); 
                                q1.remove(); 
                        } 

                        // swap the names of two queues 
                        Queue<Integer> q = q1; 
                        q1 = q2; 
                        q2 = q; 
                } 

                static void pop() 
                { 

                        // if no elements are there in q1 
                        if (q1.isEmpty()) 
                                return; 
                        q1.remove(); 
                        curr_size--; 
                } 

                static int top() 
                { 
                        if (q1.isEmpty()) 
                                return -1; 
                        return q1.peek(); 
                } 

                static int size() 
                { 
                        return curr_size; 
                } 
        } 

        // driver code 
        public static void main(String[] args) 
        { 
                Stack s = new Stack(); 
                s.push(1); 
                s.push(2); 
                s.push(3); 

                System.out.println("current size: " + s.size()); 
                System.out.println(s.top()); 
                s.pop(); 
                System.out.println(s.top()); 
                s.pop(); 
                System.out.println(s.top()); 

                System.out.println("current size: " + s.size()); 
        } 
} 

2.

For push(), we simply push the element into q1 in order of O(1)

In this approach, if we have to pop an element, we dequeue everything from q1 and push it to q2 and the last item dequeued from q1 is our desired element so we return it. then we swap the names of q1 and q2 and all this takes order of O(n) while top, isempty and size are all of order O(1)

import java.util.*; 

class Main { 
        Queue<Integer> q1 = new LinkedList<>(), q2 = new LinkedList<>(); 
        int curr_size; 

        public Stack() 
        { 
                curr_size = 0; 
        } 

        void remove() 
        { 
                if (q1.isEmpty()) 
                        return; 

                // Leave one element in q1 and 
                // push others in q2. 
                while (q1.size() != 1) { 
                        q2.add(q1.peek()); 
                        q1.remove(); 
                } 

                // Pop the only left element 
                // from q1 
                q1.remove(); 
                curr_size--; 

                // swap the names of two queues 
                Queue<Integer> q = q1; 
                q1 = q2; 
                q2 = q; 
        } 

        void add(int x) 
        { 
                q1.add(x); 
                curr_size++; 
        } 

        int top() 
        { 
                if (q1.isEmpty()) 
                        return -1; 

                while (q1.size() != 1) { 
                        q2.add(q1.peek()); 
                        q1.remove(); 
                } 

                // last pushed element 
                int temp = q1.peek(); 

                // to empty the auxiliary queue after 
                // last operation 
                q1.remove(); 

                // push last element to q2 
                q2.add(temp); 

                // swap the two queues names 
                Queue<Integer> q = q1; 
                q1 = q2; 
                q2 = q; 
                return temp; 
        } 

        int size() 
        { 
                return curr_size; 
        } 

        // Driver code 
        public static void main(String[] args) 
        { 
                Stack s = new Stack(); 
                s.add(1); 
                s.add(2); 
                s.add(3); 
                s.add(4); 

                System.out.println("current size: " + s.size()); 
                System.out.println(s.top()); 
                s.remove(); 
                System.out.println(s.top()); 
                s.remove(); 
                System.out.println(s.top()); 
                System.out.println("current size: " + s.size()); 
        } 
} 

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
3.1 Implement the stack ADT using array (4 marks) 3.1.1 Implement the pop() operation in the...
3.1 Implement the stack ADT using array 3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array using array. The starter code is as follows. The instance variables and most operations are provided. You need to implement the pop operation. Make sure that your program checks whether the stack is empty in the pop operation. import java . io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
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): #...
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.
Q1 A- What are stack and queue? What are the differences between stack and queue? B-...
Q1 A- What are stack and queue? What are the differences between stack and queue? B- What is the priority queue? What are the differences between queue and priority queue
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using...
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using the adjacency matrix structure. LANGUAGE IS IN JAVA Comment for any questions Data structures and algorithms
Implement the stack ADT in a fully generic manner (through the use of templates) by means...
Implement the stack ADT in a fully generic manner (through the use of templates) by means of a singly linked list. (Give your implementation “from scratch,” without the use of any classes from the Standard Template Library ).
Using the Queue ADT: Create a program that uses a Queue. Your program should ask the...
Using the Queue ADT: Create a program that uses a Queue. Your program should ask the user to input a few lines of text and then outputs strings in same order of entry. (use of the library ArrayDeque) In Java please.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT