The code which contains a proper definition of a doubleended

The code which contains a proper definition of a double-ended queue (written Deque and pronounced \"deck\"). A Deque is like a Queue, but includes methods to add and remove from both ends of the structure. Use the store to hold elements, complete the 4 methods adding and removing data. You will need the EmptyDequeException to throw when errors occur.

Given EmptyDequeException:

Code:

Solution

Dear Friend the question which you posted in the site is not much clear at starting you said you want\'s to implement DQueue but at the at last you said  \'Use the store to hold elements, complete the 4 methods adding and removing data\' .so my focus was only for complete mentioned four methods , but if you need complete DQueue please write me i will try to fullfill your requirement..

Thanks..

Code of EmptyDequeException class is mention below.

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

package edu.buffalo.cse116;

public class EmptyDequeException extends RuntimeException{
public EmptyDequeException() {
super();
}

public EmptyDequeException(String message) {
super(message);
}
}

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

Code of ProperDeque<E> class is mention below

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

package edu.buffalo.cse116;

import java.util.ArrayList;
import java.util.List;

public class ProperDeque<E> {
private List<E> store;

public ProperDeque() {
this.store = new ArrayList<>();
}
/**
* Adds an item onto the deque in a manner similar to that of pushing data
* onto a stack.
*
* @param item Element to be added to the front of the deque
*/
public void addFront(E item) {
System.out.println(\"adding at front: \"+item);
store.add(0,item);
//System.out.println(store);
}
/**
* Adds an item onto the deque in a manner similar to that of enqueueing data
* onto a queue.
*
* @param item Element to be added to the rear of the deque
*/
public void addLast(E item) {
System.out.println(\"adding at rear: \"+item);
store.add(item);
//System.out.println(store);
}
/**
* Removes an item from the end of the deque. This method is unique to a
* deque.
*
* @return Element that was removed from the end of the deque
* @throws EmptyDequeException Exception thrown when this method is called on
* an empty deque
*/
public E removeLast() throws edu.buffalo.cse116.EmptyDequeException {
if(store.isEmpty()){
throw new edu.buffalo.cse116.EmptyDequeException(\"Deque underflow!! unable to remove.\");
  
}
return (E) store.listIterator();

}
/**
* Returns the number of elements in this double-ended queue.
*
* @return Items available in the double-ended queue.
*/
public int size() {
return store.size();
}
/**
* Returns whether there are any elements in this Stack.
*
* @return True if the Stack does not have any elements; false otherwise.
*/
public boolean isEmpty() {
return store.isEmpty();
}

/**
* Removes all the elements from the Stack.
*/
public void clear() {
store.clear();
}
public static void main(String a[]){

edu.buffalo.cse116.ProperDeque deq = new edu.buffalo.cse116.ProperDeque<>();
System.out.println(deq.size());
System.out.print(deq.isEmpty());
System.out.println();
deq.removeLast();//----------------->please make comment for checking the exception calling here
System.out.println();
deq.addFront(34);
deq.addFront(35);
deq.addFront(36);
deq.addLast(deq.store);
//deq.removeLast();
  
deq.clear();
}
}

The code which contains a proper definition of a double-ended queue (written Deque and pronounced \
The code which contains a proper definition of a double-ended queue (written Deque and pronounced \
The code which contains a proper definition of a double-ended queue (written Deque and pronounced \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site