Question

Using Java: Provide an implementation of the LinkedQueue class by implementing it using two stacks. Submit...

Using Java:

Provide an implementation of the LinkedQueue class by implementing it using two stacks. Submit your implementation on a java class/file called Queue2Stack.jar. You can use the implementations of stacks provided by the Java API for this exercise or you can use the LinkedStack class we implemented in class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In case of any query, do comment. Please rate your answer.

Code is as below:

===================Queue2Stack.java===================

import java.util.*;

public class Queue2Stack

{

private Stack <Integer> rearStack = new Stack <Integer> ();

private Stack <Integer> frontStack = new Stack <Integer> ();

public Queue2Stack()

{

     rearStack = new Stack<>();

     frontStack = new Stack<>();

}

//add an item into the queue

public void enqueue(int x)

{

    // Enqueue will happen to rear end of the queue which is so we have to move all elements from rearStack to frontStack

    while (!rearStack.isEmpty ())

      {

                  //pop an element one by one from rear stack and push to frontStack

                  frontStack.push (rearStack.pop ());

      }

    //push it to rearstack

    rearStack.push (x);

    // then push back all items of frontStack to rearStack again

    while (!frontStack.isEmpty ())

      {

              rearStack.push (frontStack.pop ());

      }

}

// remove an item from the queue

public int dequeue()

{

    // if first stack is empty

    if (rearStack.isEmpty ())

      {

              System.out.println ("You can't remove an element from Empty Queue");

              return -1;

      }

    // Return top of rearStack

    int item = rearStack.peek ();

    rearStack.pop ();

    return item;

}

}

=================main driver program======================

public class Main

{

    public static void main(String[] args)

    {

        //create an object for Queue2Stack

        Queue2Stack queue2Stack = new Queue2Stack();

      

        //enqueue 3 items in the queue

        queue2Stack.enqueue(11);

        queue2Stack.enqueue(23);

        queue2Stack.enqueue(35);

        //dequeue the item and print it on console

        System.out.println(queue2Stack.dequeue());

        System.out.println(queue2Stack.dequeue());

        System.out.println(queue2Stack.dequeue());

        queue2Stack.dequeue(); //will show stack empty message

    }

}

===============screen shot of the code===============

Main.java Queue2Stack.java: 1 import java.util.; 3 public class Queue2Stack 4- { private Stack <Integer> rearStack = new Stac

Main.java Queue Stack.java: 1 public class Main 2 - { public static void main(String[] args), //create an object for Queue2St

Output:

input 23 35 You cant remove an element from Empty Queue -.. Program finished with exit code o Press ENTER to exit console.

Add a comment
Know the answer?
Add Answer to:
Using Java: Provide an implementation of the LinkedQueue class by implementing it using two stacks. Submit...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The class pictured below is designed to implement an integer queue using two stacks. Assume the...

    The class pictured below is designed to implement an integer queue using two stacks. Assume the stack methods all work as desired (though their implementations are not shown). .(a) Trace what happens in the following situation, showing intermediate steps (values of variables, what is in the stacks, and what is in the queue at various points in the methods, not just the results of the methods). • Start with an empty queue. Enqueue 5, then 16, then 7. Dequeue twice....

  • JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity...

    JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity = 4; private Stack stack1; private Stack stack2; Note: You can use library Stack but you are not allowed to use library Queue and any of its methods Your Queue should not accept null or empty String or space as an input You need to implement the following methods using two stacks (stack1 & stack2) and also you can add more methods as well: public...

  • In this assignment you will practice using Data Structures and Object Oriented concepts in Java. Your implementation should target the most efficient algorithms and data structures. You will be graded...

    In this assignment you will practice using Data Structures and Object Oriented concepts in Java. Your implementation should target the most efficient algorithms and data structures. You will be graded based on the efficiency of your implementation. You will not be awarded any points if you use simple nested loops to implement the below tasks. You should use one or more of the below data structures: - ArrayList : - JavaDoc: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html - Tutorial: http://docs.oracle.com/javase/tutorial/collections/interfaces/list.html Question You are provided with...

  • This lab serves as an intro to Java Interfaces and an Object-Oriented design strategy towards implementing...

    This lab serves as an intro to Java Interfaces and an Object-Oriented design strategy towards implementing data structures. We will be using Entry.java objects which are key,value pairs, with key being an integer and value being a generic parameter. We will be sorting these Entries according to their key value. The file Entry.java is given to you and does not require any modification. As you should know by now, bucket sort works by placing items into buckets and each bucket...

  • We are using blueJ for my java class, if you can help me id greatly appreciate...

    We are using blueJ for my java class, if you can help me id greatly appreciate it. Create a new Java class called AverageWeight. Create two arrays: an array to hold 3 different names and an array to hold their weights. Use Scanner to prompt for their name and weight and store in correct array. Compute and print the average of the weights entered using printf command. Use a loop to traverse and print the elements of each array or...

  • 42) Create a toString method for the LinkedStack class. This method should create and return a...

    42) Create a toString method for the LinkedStack class. This method should create and return a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the LinkedStack class and for testing and debugging applications that use the LinkedStack class. 46) Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The...

  • Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The...

    Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The application will be to add large numbers. Review adding large numbers Remember that we can use stacks to safely add integer values that overflow the int data type g. in Java, the maximum possible int value Integer.MAX_VALUE is: 2147483647 so any int addition larger than this will overflow and fail Using stacks to add large numbers safely Will actually represent the large integers to...

  • In Java The following Java implementation of a class Node is given: private class Node<Object> {...

    In Java The following Java implementation of a class Node is given: private class Node<Object> { Node() { this(null, null); } Node(Object d) { this(d, null); } Node(Object d, Node n) { data = d; next = n; } Object data; Node next; } Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a reference to the header node. Using the class Node described above, write a MySingleLinkedList...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • Go to the Java API and review the Rectangle class briefly. As directed below, create a...

    Go to the Java API and review the Rectangle class briefly. As directed below, create a subclass of the Rectangle class called BetterRectangle as described below. Create another class called RectangleTester, which fully tests the BetterRectangle, by displaying a menu that allows a user to process data for multiple rectangles until they choose to quit. Be sure to include sample runs as block comments in your tester source code file. P9.10 The Rectangle class of the standard Java library does...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT