Question

In: Computer Science

In java thanks! Design a Stack that is composed ONLY of one or two Queue objects...

In java thanks!

Design a Stack that is composed ONLY of one or two Queue objects ergo the ONLY instance variables that exist in this stack are queues.

Stack class should contain the following methods:

Print
Pop
Push
Top
Size
isEmpty
copy

[(2)] Design a Queue that is composed ONLY of two Stacks objects ergo the ONLY instance variables that exist in this queue are stacks.

Queue class should contain the following methods:

  Print
Enqueue
Dequeue
Front
Rear
Size
isEmpty
Copy

Solutions

Expert Solution

I have implemented some of the methods and all of the basic code for starting. And left some of the really simple ones for you to code by yourself I have also mentioned the comments on required lines.

Here goes the code:

import java.util.*; 
class Stack_Using_Queues { 

        static class Stack { 
                static Queue<Integer> q1 = new LinkedList<Integer>();
        static Queue<Integer> q2 = new LinkedList<Integer>();
                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 (q1.isEmpty()) 
                                return; 
                        q1.remove(); 
                        curr_size--; 
                } 

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

                static int size() 
                { 
                        return curr_size; 
                } 
        } 
        public static void main(String[] args) 
        { 
                Stack s = new Stack(); 
                s.push(13); 
                s.push(25); 
                s.push(38); 
                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()); 
        } 
} 

import java.util.*; 
class Queue_Using_Stack 
{ 
static class Queue 
{ 
        static Stack<Integer> s1 = new Stack<Integer>(); 
        static Stack<Integer> s2 = new Stack<Integer>(); 
        static void enQueue(int x) 
        { 
                // Move all elements from s1 to s2 
                while (!s1.isEmpty()) 
                { 
                        s2.push(s1.pop()); 
                        //s1.pop(); 
                } 

                // Push item into s1 
                s1.push(x); 

                // Push everything back to s1 
                while (!s2.isEmpty()) 
                { 
                        s1.push(s2.pop()); 
                        //s2.pop(); 
                } 
        } 

        // Dequeue an item from the queue 
        static int deQueue() 
        { 
                // if first stack is empty 
                if (s1.isEmpty()) 
                { 
                        System.out.println("Q is Empty"); 
                        System.exit(0); 
                } 

                // Return top of s1 
                int x = s1.peek(); 
                s1.pop(); 
                return x; 
        } 
}; 

public static void main(String[] args) 
{ 
        Queue q = new Queue(); 
        q.enQueue(16); 
        q.enQueue(27); 
        q.enQueue(89);
    q.enQueue(76); 
    q.enQueue(65); 
        System.out.println(q.deQueue()); 
        System.out.println(q.deQueue()); 
        System.out.println(q.deQueue()); 
} 
} 

Related Solutions

Java [(1)] Design a Stack that is composed ONLY of one or two Queue objects ergo...
Java [(1)] Design a Stack that is composed ONLY of one or two Queue objects ergo the ONLY instance variables that exist in this stack are queues. Stack class should contain the following methods: Print, Pop, Push, Top, Size, isEmpty, copy [(2)] Design a Queue that is composed ONLY of two Stacks objects ergo the ONLY instance variables that exist in this queue are stacks. Queue class should contain the following methods: Print, Enqueue, Dequeue, Front, Rear, Size, isEmpty, Copy...
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): #...
In this lab, using C++, you will create two data structures: a stack and a queue....
In this lab, using C++, you will create two data structures: a stack and a queue. You will use STL containers to demonstrate basic ADTs. Queue For the queue, you will simulate a buffer. Remember it is first-in-first-out. The user will enter a number for the number of rounds to run your simulation. You need one function that randomly generates a number. You will also have a user specified percentage, and the function uses this percentage to randomly put the...
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 Java) Design a stack that supports getMin(), pop() and push() in O(1) time. Must use...
(In Java) Design a stack that supports getMin(), pop() and push() in O(1) time. Must use the iterator and comparator, does not need to be a linked list, although it's what I've tried using. I'm using another tester class to test this class. import java.util.Comparator; import java.util.List; import java.util.LinkedList; import java.util.Iterator; import java.util.Stack; import java.util.ListIterator; public class FMinStack<T> implements MinStack<T> { protected Comparator<? super T> comp; T min; protected List<T> ds; public FMinStack() { this(new DefaultComparator<T>()); } public FMinStack(Comparator<? super...
Java programming! Implement a static stack class of char. Your class should include two constructors. One...
Java programming! Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Change this cods according to instructions please: public class Stack { int stackPtr; int data[]; public Stack() //constructor { stackPtr=0;...
Create a windows application that contains two TextBox objects and two Button objects. One of the...
Create a windows application that contains two TextBox objects and two Button objects. One of the TextBox objects and one of the buttons are initially invisible. The first textbox should be used to input a password. The textbox should be masked to some character of your choice so that the characters entered by the user are not seen on the screen. When the user clicks the first button, the second TextBox object and button object should be displayed with a...
Introduced to data structure using java 2)One of the applications of stack is to convert infix...
Introduced to data structure using java 2)One of the applications of stack is to convert infix expressions to postfix. Given the following infix expression, use the stack method to convert it to postfix. Show your work. x - y / (a + b) + z 3)write a generic method to find the maximum of three variables of generic type (i.e. type T). Note that you must use the compareTo method to compare the values. Also, the code must return one...
Consider the effects of inflation in an economy composed of only two people: Jake, a bean...
Consider the effects of inflation in an economy composed of only two people: Jake, a bean farmer, and Latasha, a rice farmer. Jake and Latasha both always consume equal amounts of rice and beans. In 2016 the price of beans was $1, and the price of rice was $4. Suppose that in 2017 the price of beans was $2 and the price of rice was $8. Inflation was ______% . Indicate whether Jake and Latasha were better off, worse off,...
Consider the effects of inflation in an economy composed of only two people: Manuel, a bean...
Consider the effects of inflation in an economy composed of only two people: Manuel, a bean farmer, and Poornima, a rice farmer. Manuel and Poornima both always consume equal amounts of rice and beans. In 2016 the price of beans was $1, and the price of rice was $4. Suppose that in 2017 the price of beans was $2 and the price of rice was $8. Inflation was __%? . Indicate whether Manuel and Poornima were better off, worse off,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT