Code: public interface DequeInterface<T> { public void addToFront(T newEntry); p
ID: 3714820 • Letter: C
Question
Code: public interface DequeInterface<T>
{
public void addToFront(T newEntry);
public void addToBack(T newEntry);
public T removeFront();
public T removeBack();
public T getFront();
public T getBack();
public boolean isEmpty();
public void clear();
} // end DequeInterface
Question:
Show the contents of two initially empty deques after each of the following statements execute. Assume x, y, z, a, and b are objects.
Your answer should have 11 lines that each show the contents of dq1 and dq2.
Put the front of the deque on the left.
dq1.addToFront(x)
dq1.addToFront(y)
dq1.addToBack(z)
dq2.addToFront(a)
dq2.addToBack(b)
dq2.addToBack(dq1.removeBack())
dq1.addToFront(dq2.getBack())
dq1.addToFront(dq2.getBack())
dq2.addToFront(dq2.removeFront())
dq1.addToBack(dq2.getFront())
dq2.addToBack(dq2.removeFront());
Explanation / Answer
import java.util.ArrayList;
import java.util.List;
interface DequeInterface<T>
{
public void addToFront(T newEntry);
public void addToBack(T newEntry);
public T removeFront();
public T removeBack();
public T getFront();
public T getBack();
public boolean isEmpty();
public void clear();
} // end DequeInterface
public class DoubleEndedQueueImpl implements DequeInterface<Integer>{
private List<Integer> deque = new ArrayList<Integer>();
@Override
public void addToFront(Integer item){
//add element at the beginning of the queue
System.out.println("adding at front: "+item);
deque.add(0,item);
System.out.println(deque);
}
@Override
public void addToBack(Integer item){
//add element at the end of the queue
System.out.println("adding at rear: "+item);
deque.add(item);
System.out.println(deque);
}
@Override
public Integer removeFront(){
if(deque.isEmpty()){
System.out.println("Deque underflow!! unable to remove.");
return 0;
}
//remove an item from the beginning of the queue
int rem = deque.remove(0);
System.out.println("removed from front: "+rem);
System.out.println(deque);
return rem;
}
@Override
public Integer removeBack(){
if(deque.isEmpty()){
System.out.println("Deque underflow!! unable to remove.");
return 0;
}
//remove an item from the beginning of the queue
int rem = deque.remove(deque.size()-1);
System.out.println("removed from front: "+rem);
System.out.println(deque);
return rem;
}
@Override
public Integer getFront(){
//gets the element from the front without removing it
int item = deque.get(0);
System.out.println("Element at first: "+item);
return item;
}
@Override
public Integer getBack(){
//gets the element from the rear without removing it
int item = deque.get(deque.size()-1);
System.out.println("Element at rear: "+item);
return item;
}
@Override
public boolean isEmpty() {
if(deque.isEmpty()) return true;
else return false;
}
@Override
public void clear() {
deque =null;
System.out.println("Deque is cleared now!!");
}
public static void main(String a[]){
DoubleEndedQueueImpl dq1 = new DoubleEndedQueueImpl();
DoubleEndedQueueImpl dq2 = new DoubleEndedQueueImpl();
int x=5,y=9,z=4;
int p=6,b=3;
dq1.addToFront(x);
dq1.addToFront(y);
dq1.addToBack(z);
dq2.addToFront(p);
dq2.addToBack(b);
dq2.addToBack(dq1.removeBack());
dq1.addToFront(dq2.getBack());
dq1.addToFront(dq2.getBack());
dq2.addToFront(dq2.removeFront()) ;
dq1.addToBack(dq2.getFront());
dq2.addToBack(dq2.removeFront());
}
}
/* output:-
adding at front: 5
[5]
adding at front: 9
[9, 5]
adding at rear: 4
[9, 5, 4]
adding at front: 6
[6]
adding at rear: 3
[6, 3]
removed from front: 4
[9, 5]
adding at rear: 4
[6, 3, 4]
Element at rear: 4
adding at front: 4
[4, 9, 5]
Element at rear: 4
adding at front: 4
[4, 4, 9, 5]
removed from front: 6
[3, 4]
adding at front: 6
[6, 3, 4]
Element at first: 6
adding at rear: 6
[4, 4, 9, 5, 6]
removed from front: 6
[3, 4]
adding at rear: 6
[3, 4, 6]
*/