Double Ended Queue Dqueue A double ended queue is much like
Double Ended Queue: (Dqueue) A double ended queue is much like a queue except that the operations of enqueing (adding) and dequeing (removing) can be done at both ends of the queue. In a conventional queue, the enque operation is done at the tail and deque is done at the tail but in a Dqueue these operations are allowed at both ends. The following 6 methods are possible.
public void enqueHead(E element),
public void enqueTail(E element)
public E dequeHead()
public E dequeTail()
public E peekHead() that returns the element at the Head but not remove it.
public E peekTail() that returns the element at the Tail but not remove it.
The goal is to develop a java program to implement a Dqueue in an efficient manner for all the above 6 methods. Extend java.util.LinkedList <E> to Dqueue<E> and implement all of the above methods.
Solution
Here is the code for you:
import java.util.LinkedList;
class DQueue<E>
{
LinkedList<E> queue;
public DQueue()
{
queue = new LinkedList();
}
public void enqueHead(E element) //Adds element to the head of the queue.
{
queue.add(1, element);
}
public void enqueTail(E element) //Adds element to the tail of the queue.
{
queue.add(element);
}
public E dequeHead() //Removes element from the head of the queue.
{
if(queue.size() == 0)
return null;
return queue.removeFirst();
}
public E dequeTail() //Removes element from the tail of the queue.
{
if(queue.size() == 0)
return null;
return queue.removeLast();
}
public E peekHead() //that returns the element at the Head but not remove it.
{
if(queue.size() == 0)
return null;
return queue.peekFirst();
}
public E peekTail() //that returns the element at the Tail but not remove it.
{
if(queue.size() == 0)
return null;
return queue.peekLast();
}
}

