The interface Deque can have array based and linked implementations. Partial ver
ID: 3791978 • Letter: T
Question
The interface Deque can have array based and linked implementations. Partial versions of implementation classes follow. Supply implementations for the method addRear for both. class ArrayDeque implements Deque//circular array based {private object data []; private int front, rear, size, capacity public ArrayDeque () {capacity = 1000; data = new Object [capacity]; front = size 0; rear = 1;}//method code omitted here} class DNode {private Object data; private DNode prev, next;//standard constructors, get and set method code omitted here} class LinkedDeque implements Deque {private DNode front, rear; private int size; public LinkedDeque () {front = rear = null; size = 0;}//method code omitted here Implement the method addRear for the array based class. Implement the method addRear for the linked class.Explanation / Answer
Hi buddy, Please find the below java program. I've added comments for your better understanding
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.ArrayList;
class ArrayDequeue implements Dequeue{
private Object data[];
private int front,rear,size,capacity;
public ArrayDequeue(){
capacity = 1000;
data = new Object[capacity];
front = size = 0;
rear = 1;
}
public void addRear(Object obj){
//Add the object to the end of the Dequeue.
data[rear-1] = obj;
//Increment rear
rear++;
//Increment the size of Dequeue
size++;
}
}
class DNode{
private Object data;
private DNode prev,next;
//This Constructor sets the data
public DNode(Object data){
this.data = data;
}
//This method returns the data
public Object getNode(){
return this.data;
}
}
class LinkedDequeue implements Dequeue{
private DNode front,rear;
private int size;
public LinkedDequeue(){
front = rear = null;
size = 0;
}
public void addRear(Object obj){
//Create a new Node
DNode d = new DNode(obj);
rear.next = d;
d.prev = rear;
rear = d;
//If front is null , then front and rear are the same node
if(front==null){
front = rear;
}
//Increment the size;
size++;
}
}
class Main {
public static void main (String[]args) throws FileNotFoundException {
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.