/*************** Homework C Edit the following file and save it as Cxxxx.java where xxxx is your 4 digit ID number. Send email to ryba@venus.cs.qc.cuny.edu from your account on venus. Add an attachment with your file Cxxxx.java The file C0000.java contains an interface Deque --- no changes should be made to this an implementation class ArrayDeque --- no changes are to be made to this a class DNode for doubly linked nodes --- no changes should be made to this an implementation class LinkedDeque --- the methods of this class are missing. The homeowork assignment is to complete the methods: LinkedDeque(), size(), empty(), removeFront(), removeRear(), addRear(Object x), addFront(Object x), toString(), main(String args[]). You should adjust the main method to display your name and ID number. The only change needed in main() is to make it print your name in addition to the other tasks which are already programmed. In the other methods, you should supply an implementation using doubly linked nodes in such a way that a client would not see a difference between the operation of the classes ArrayDeque and Linked Deque. You must not add any additional instance variables to the class LinkedDeque. ********************************************************************/ import java.util.Scanner; interface Deque { public Object removeFront(); public Object removeRear(); public void addFront(Object x); public void addRear(Object x); public boolean empty(); public int size(); } class ArrayDeque implements Deque { private Object data[]; private int front, rear, size, capacity; public ArrayDeque() { capacity = 1000; data = new Object[capacity]; front = size = 0; rear = 1; } public ArrayDeque(int c) { capacity = c; data = new Object[capacity]; front = size = 0; rear = 1; } public int size() { return size; } public boolean empty() { return size == 0; } public void addFront(Object x) { if (size() == capacity) throw new RuntimeException("Deque Full"); data[front--]= x; if (front < 0) front = capacity - 1; size ++; } public void addRear(Object x) { if (size() == capacity) throw new RuntimeException("Deque Full"); data[rear++] = x; if (rear == capacity) rear = 0; size ++; } public Object removeFront() { if (empty()) throw new RuntimeException("Deque Empty"); front = front + 1; if (front == capacity) front = 0; size --; return data[front]; } public Object removeRear() { if (empty()) throw new RuntimeException("Deque Empty"); rear = rear - 1; if (rear == -1) rear = capacity - 1; size --; return data[rear]; } // methods for testing purposes public String toString() { int i, j; String ans = "Array Deque: " + front +","+rear+";"+size + " :: " ; for (i = 0, j = front + 1; i< size; i++, j++) { if (j == capacity) j = 0; ans += ( data[j] + " -> "); } return ans; } public static void main(String args[]) { System.out.println("Homework C --- Spring 2008 version."); Deque q = new ArrayDeque(); testDeque(q); } public static void testDeque(Deque q) { boolean done = false; while (!done) { try { Scanner sc = new Scanner(System.in); System.out.print( q + " "); System.out.println("\ncmds are a r --- A R Q: >>"); String cmd = sc.next(); String entry = null; char command = cmd.charAt(0); if (command == 'a' || command == 'A') entry = sc.next(); switch (cmd.charAt(0)) { case 'Q': done = true; break; case 'a': q.addFront(entry); break; case 'A': q.addRear(entry); break; case 'r': q.removeFront(); break; case 'R': q.removeRear(); break; } } catch (Exception e) { System.out.println("Error " + e.toString()); } } } } class DNode { private Object data; private DNode prev, next; public DNode(Object x, DNode p, DNode n) { data = x; prev = p; next = n; } public Object getData() { return data; } public DNode getNext() { return next; } public DNode getPrev() { return prev; } public void setData(Object x) { data = x; } public void setNext(DNode n) { next = n; } public void setPrev(DNode p) { prev = p; } } class LinkedDeque implements Deque { private DNode front, rear; private int size; // add implementations for the following methods // your code should perform in exactly the same way as the corresponding ArrayDeque methods // you should not add any extra instance variables to the class and must // use a doubly linked list implementation. public LinkedDeque() { // add implementation } public int size() { // add implementation } public boolean empty() { // add implementation } public Object removeFront() { // add implementation } public Object removeRear() { // add implementation } public void addRear(Object x) { // add implementation } public void addFront(Object x) { // add implementation } public String toString() { // add implementation } public static void main(String args[]) { System.out.println("Homework C by xxxxxx "); // fill in your name here Deque q = new LinkedDeque(); ArrayDeque.testDeque(q); } }