Question

(java) Please implement your own Dequeue class which has following methods •boolean add(E e)= void addLast(E...

(java)

Please implement your own Dequeue class which has following methods

•boolean add(E e)= void addLast(E e)  // two methods are the same. You could implement either one

•void addFirst(E e)

•E getFirst( ) = E peek( ) // two methods are the same. You could implement either one

•E getLast( )

•E removeFirst()

•E removeLast()

Case 1 : Queue

•Create a queue which is an instance of Dequeue. The queue should perform with given following input and print the correct output.

•Input format

•The first line contains an integer of q :

•q specifies the number of queries which follows in the next lines

•Each of the q next  lines contains a single query in the form of one of following 3 types :

•1 x: Enqueue element  into the end of the queue.

•2: Dequeue the element at the front of the queue.

•3: Print the element at the front of the queue.

Case 2. Stack

•Create a a stack which is an instance of Dequeue. The stack should perform with given following input and print the correct output.

•Input format

•Any string of postfix expression

•Example “34+78+*”

•Output

•The value of evaluation

•105

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

Program:

import java.util.*;
import java.io.*;

//Dequeue class
class Dequeue<E>
{
   //data members
   private int first;
   private int last;
   private int size;
   private E dq[];
   //static data members
   static int MAX = 10;
  
   //constri=uctor
   public Dequeue()
   {
       first = -1;
       last = -1;
       size = 0;
       dq = (E[]) new Object[MAX];
   }
  
   //add an item to the last
   void addLast(E e) throws IOException
   {
       if(size==MAX)
           throw new IOException();
      
       size++;
       if(last==-1)
       {
           first = last = 0;
       }
       else
           last = (last+1)%size;
       dq[last] = e;
   }
   //add an item to the first
   void addFirst(E e) throws IOException
   {
       if(size==MAX)
           throw new IOException();
      
       size++;
       if(first==-1)
       {
           first = last = 0;
       }
       else if (first == 0)
first = size-1;
else
   first = first-1;
  
dq[first] = e;
   }
   //return an item from the first
   E getFirst() throws IOException
   {
       if(size==0)
           throw new IOException();
      
       return dq[first];
   }
   //return an item from the last
   E getLast() throws IOException
   {
       if(size==0)
           throw new IOException();
      
       return dq[last];
   }
   //remove an item from the first
   E removeFirst() throws IOException
   {
       if(size==0)
           throw new IOException();
       E item = dq[first];;
      
       if(first==last)
       {
           first = last = -1;
       }
       else if(first == size-1)
       {
           first=0;
       }
       else
           first= first +1;
          
       return item;
   }
   //remove an item from the last
   E removeLast() throws IOException
   {
       if(size==0)
           throw new IOException();
       E item = dq[last];;
      
       if(first==last)
       {
           first = last = -1;
       }
       else if(last == 0)
       {
           last = size-1;
       }
       else
           last = last-1;
          
       return item;
   }
}

//class Tester
class Tester
{
   //method to test case 1 for queue operations
   static void queueCaseOne() throws IOException
   {
       //create a queue
       Dequeue<Integer> queue = new Dequeue<Integer>();
       //create an instance of Scanner class
       Scanner sc = new Scanner(System.in);
      
       //number of queries
       System.out.println ("Enter number of queries");
       int q = sc.nextInt();
      
       //execute q number of queries
       for(int i=0; i<q; i++)
       {
           System.out.println ("1. Enqueue element into the end of the queue.");
           System.out.println ("2. Dequeue the element at the front of the queue.");
           System.out.println ("3. Print the element at the front of the queue.");
           int op = sc.nextInt();
          
           int x;
           switch(op)
           {
               case 1:
                   x = sc.nextInt();
                   queue.addLast(x);
                   break;
               case 2:
                   x = queue.removeFirst();
                   System.out.println (x + " is removed");
                   break;
               case 3:
                   x = queue.getFirst();
                   System.out.println ("The element at the front is " + x);
                   break;
           }
       }
   }
   //method to test case 2 for stack operations
   static void stackCaseTwo() throws IOException
   {
       //create a stack
       Dequeue<Integer> stack = new Dequeue<Integer>();
      
       //postfix expression
       String postfix = "34+78+*";
      
       //evaluate postfix expression using stack
       for(int i=0; i<postfix.length(); i++)
       {
           char ch = postfix.charAt(i);
          
           //check for operand
           if(Character.isLetterOrDigit(ch))
           {
               stack.addLast(ch - '0');
           }
           //when operator found
           else
           {
               //pop two items from the stack
               int y = stack.removeLast();
               int x = stack.removeLast();
               //perform operation
               switch(ch)
               {
                   case '+': stack.addLast(x+y);
                               break;
                   case '-': stack.addLast(x-y);
                               break;
                   case '*': stack.addLast(x*y);
                               break;
                   case '/': stack.addLast(x/y);
                               break;
               }
              
           }
       }
       //display the output
       System.out.println ("Output = " + stack.removeLast());
   }
  
   //main method
   public static void main (String[] args) throws IOException
   {
       System.out.println ("Test case 1 for queue operations:\n");
       queueCaseOne();
       System.out.println ("\n\nTest case 2 for stack operations:\n");
       stackCaseTwo();
   }
}

Output:

Test case 1 for queue operations:

Enter number of queries
5
1. Enqueue element into the end of the queue.
2. Dequeue the element at the front of the queue.
3. Print the element at the front of the queue.
1 5
1. Enqueue element into the end of the queue.
2. Dequeue the element at the front of the queue.
3. Print the element at the front of the queue.
1 7
1. Enqueue element into the end of the queue.
2. Dequeue the element at the front of the queue.
3. Print the element at the front of the queue.
3
The element at the front is 5
1. Enqueue element into the end of the queue.
2. Dequeue the element at the front of the queue.
3. Print the element at the front of the queue.
2
5 is removed
1. Enqueue element into the end of the queue.
2. Dequeue the element at the front of the queue.
3. Print the element at the front of the queue.
2
7 is removed


Test case 2 for stack operations:

Output = 105

Add a comment
Know the answer?
Add Answer to:
(java) Please implement your own Dequeue class which has following methods •boolean add(E e)= void addLast(E...
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
  • My CSC 220 teacher has given this as a homework assignment and starting it has not...

    My CSC 220 teacher has given this as a homework assignment and starting it has not made much sense and am generally lost on this assignment help would be much appreciated. I put everything from the assignment power point slides she gave us in here so the expert has everything im working with as well. Dequeue Please implement your own Dequeue class which has following methods boolean add(E e)= void addLast(E e) void addFirst(E e) E getFirst( ) = E...

  • C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr...

    C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to get, set, insert and remove values at either end of the list. Call these getFirst, getLast, setFirst, setLast, insertFirst, insertLast, removeFirst, removeLast. Don't forget, you also need a copy constructor and destructor plus getLength, isEmpty and clear methods. Overload the stream insertion operator as a friend function which outputs the list in format { 1,...

  • In java Build a QueueInt class for integers that is compatible with the driver code below. The QueueInt should operate in a FIFO (first in, first out) fashion and implement the variables and methods a...

    In java Build a QueueInt class for integers that is compatible with the driver code below. The QueueInt should operate in a FIFO (first in, first out) fashion and implement the variables and methods also listed below: Data Members: Declare and initialize, as needed, the data item(s) you will need to manage a queue of integers. You may only use arrays and primitives for your instance and/or static variables (I,e You can’t use Java defined Queue / Stack / List...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • use intellij idea main java wp the professor. Please make sure to only implement what is...

    use intellij idea main java wp the professor. Please make sure to only implement what is asked for. You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here. Step 1 Develop the following interface: Interface Name: Queue Interface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue...

  • Create two Java classes called Recursive, RecursiveDemo. Write four methods a- int sum_sqr_rec(stack<int> stk) which will...

    Create two Java classes called Recursive, RecursiveDemo. Write four methods a- int sum_sqr_rec(stack<int> stk) which will receive a stack of "int" and output the sum of the squares of the elements in the stack. b- int plus_minus_rec(stack<int> stk) which will receive a stack of "int" (example: {a,b,c,d,e,f,g,h,i,j}) and output the sum of the elements in the stack as follows: a - b + c - d + e - f + g - h + i -j c- void prt_chars_rev_rec(stack<char>...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> {...

    In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element);    /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test...

    JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test it is not executing but my stack is working fine, can you fix it please! MyQueue.java Implement a queue using the MyStack.java implementation as your data structure.  In other words, your instance variable to hold the queue items will be a MyStack class. enqueue(String item): inserts item into the queue dequeue(): returns and deletes the first element in the queue isEmpty(): returns true or false...

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