Question

In: Computer Science

write JAVA code with the following condition Write the pseudocode for a new data type MyStack...

write JAVA code with the following condition

Write the pseudocode for a new data type MyStack that implements a stack using the fact that you have access to a queue data structure with operations enqueue(), dequeue(), isEmpty(). Remember that every stack should have the operations push() and pop().

Hint: use two queues, one of which is the main one and one is temporary. Please note that you won’t be able to implement both push() and pop() in constant time. One of the two will have to run in O(n), where n is the number of elements in the stack.

Solutions

Expert Solution

//Source Code:

// MyStack.java


import java.util.LinkedList;
import java.util.Queue;

class MyQueue<E>{
    Queue<E> q = new LinkedList<>();

    public void enqueue(E a){
        q.add(a);
    }

    public  E dequeue(){
        return q.poll();
    }

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

public class MyStack<E> {
    private MyQueue<E> mainQueue = new MyQueue<>();

    // This will take O(1) time
    public void push(E x){
        mainQueue.enqueue(x);
        System.out.println("Pushed " + x + " into the Stack.");
    }

    // This will take O(n) time
    public void pop(){
        MyQueue<E> tempQueue = new MyQueue<>();
        E a = null;
        while (!mainQueue.isEmpty()){
            a = mainQueue.dequeue();
            if (mainQueue.isEmpty()){
                break;
            }
            tempQueue.enqueue(a);
        }
        mainQueue = tempQueue;
        if (a == null){
            System.out.println("Stack is Empty!! Underflow.");
        }else {
            System.out.println("Poped out " + a + " from the Stack.");
        }
    }

    public static void main(String[] args) {
        MyStack<Integer> myStack = new MyStack<>();
        myStack.push(10);
        myStack.push(20);
        myStack.push(30);

        myStack.pop();
        myStack.pop();
        myStack.pop();
        myStack.pop();
    }
}

// Screenshot:

// Output:

// If you have any query do ask in the comments section.

// If you found the answer helpful do give a Thumbs Up!!


Related Solutions

write pseudocode for the following problems not c code Pseudocode only Write a C program to...
write pseudocode for the following problems not c code Pseudocode only Write a C program to print all natural numbers from 1 to n. - using while loop Write a C program to print all natural numbers in reverse (from n to 1). - using while loop Write a C program to print all alphabets from a to z. - using while loop Write a C program to print all even numbers between 1 to 100. - using while loop...
write the pseudocode and code to solve the following: you are given a list. determine the...
write the pseudocode and code to solve the following: you are given a list. determine the sum of all the elements in the list without using the built in puthon function sum(). Take your code and create your own function and name it my_sum_algo() which will return the sum of numbers PS please write comments in the code for my understanding, and please write jn jn PYTHON 3 languge. Thank you, have a great day !
Write a pseudocode for the following code: /*every item has a name and a cost */...
Write a pseudocode for the following code: /*every item has a name and a cost */ public class Item {    private String name;    private double cost;    public Item(String name, double cost) {        this.name = name;        this.cost = cost;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public double getCost() {        return cost;...
Write a java code segment to declare an array of size 10 of type String and...
Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an...
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an earthquake is in the range [0, 3), “medium” if M is in the range [3, 6), “large” if M is in the range [6, 9) and “epic” if M is greater than or equal to 9, where M is input by a user via the keyboard. (in c++)
write code with proper comments for ever step the code should be in form of pseudocode                            &
write code with proper comments for ever step the code should be in form of pseudocode                                    To Print Triangle of any size, by taking size as input. To Print Triangle of any size, by taking size as input. If size is 4 then triangle is: To calculate Factorial of any number. To calculate the power of any given number. To Print Table of Any Number up till 10: as shown in this figure. for a program which reads 10 integers...
This is an Intro to java question. Please provide code and pseudocode for better understanding. Problem...
This is an Intro to java question. Please provide code and pseudocode for better understanding. Problem 4: Player Move Overworld (10 points) (Game Development) You're the lead programmer for an indie game studio making a retro-style game called Zeldar. You've been tasked to implement the player movement. The game is top-down, with the overworld modeled as a 2d grid. The player's location is tracked by x,y values correlating to its row and column positions within that grid. Given the current...
This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA...
This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA Public Key (10 points) (Cyber Security) RSA is an asymmetric encryption scheme, where a public key is used to encrypt data and a different, private key decrypts that data. RSA public/private keys are generated from two prime numbers, typically very large ones. The idea behind RSA is based on the fact that its difficult to factorize very large integers. RSA public key generation is...
This is an Intro to Java Question, Please respond with code and pseudocode. Problem 3: RSA...
This is an Intro to Java Question, Please respond with code and pseudocode. Problem 3: RSA Private Key (10 points) (Cyber Security) In the RSA algorithm, encrypting and decrypting a message is done using a pair of numbers that are multiplicative inverses with respect to a carefully selected modulus. In this task, you must calculate the modular multiplicative inverse for given set of values. What is the Modular Multiplicative Inverse? In mathematics, each operation typically has an inverse. For example,...
Please write a java code. Write a generic program for New Home Construction Pricing with the...
Please write a java code. Write a generic program for New Home Construction Pricing with the following specifications. Note, each one of the 2, 3 or 4 bedroom homes are priced with standard type bathrooms. Update to deluxe or premium choice of bathrooms can be ordered by paying the difference in prices.    Types of homes Price 2 bedroom, 2 bathroom (standard type) and 1 car garage home = $350,000 3 bedroom, 2 bathroom (standard type) and 2 car garage...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT