Question
I have the following Java Deque (Double Ended Queue) that does not seem to be working properly. I am just learning about these types of
I have the following Java Deque (Double Ended Queue) that does not seem to be working properly. I am just learning about these types of queues and came up with the following code:
import java.util.Scanner;
public class MyDeque { private Item[] deque;
private static int head; private static int size;
public MyDeque() {
deque = (Item[]) new Object[10]; }
public MyDeque(int n) { deque = (Item[]) new Object[n]; }
public void addToFront(Item item) { checkCapacity(); if(head == 0) { deque[(head = deque.length - 1)] = item; } else { deque[(--head) % deque.length] = item; } size++; }
public void addToBack(Item item) { checkCapacity(); deque[(head + size++) % deque.length] = item; }
public Item getFirst() throws EmptyDequeException { int tempFloor = (int)Math.floor(deque.length/4); if(size < tempFloor && deque.length >= 20) { // Using 20 to compare because anything less would result in capacity dropping below 10 // reduce array capacity by 1/2 changeArrayCapacity(0.5); } Item temp = deque[head]; head = (head + 1) % deque.length; size--; return temp; }
public Item getLast() throws EmptyDequeException { int tempFloor = (int)Math.floor(deque.length/4); if(size < tempFloor && deque.length >= 20) { //reduce array capacity by 1/2 changeArrayCapacity(0.5); } Item temp = deque[(head + size - 1) % deque.length]; size--; return temp; }
public boolean isEmpty() {
if(size == 0) { return true; } return false; }
public int size() { return size; }
public Item lookFirst() throws EmptyDequeException { return deque[head]; }
public Item lookLast() throws EmptyDequeException { return deque[(head + size - 1) % deque.length]; }
public Item[] getArray() {
return deque; } private void changeArrayCapacity(double amount) { Item[] newDeque = (Item[]) new Object[(int) Math.floor(deque.length * amount)]; System.arraycopy(deque, 0, newDeque, 0, deque.length); deque = newDeque; } private void checkCapacity() { if(size >= deque.length) { changeArrayCapacity(2); } System.out.println("New size: " + deque.length); } private void test() { String again = "y"; Scanner in = new Scanner(System.in); do {
System.out.println("Enter a word to add to the deque: "); String temp = in.nextLine(); Item integerToAdd = (Item) temp; System.out.println("Insert into front or back?"); String frontOrBack = in.nextLine(); if(frontOrBack.equals("front")) { addToFront(integerToAdd); } else if(frontOrBack.equals("back")) { addToBack(integerToAdd); } System.out.println("Add another? y or n"); again = in.nextLine(); }while(again.equals("y")); if(again.equals("n")) { try { getChoice(); } catch (EmptyDequeException e) { // TODO Auto-generated catch block e.printStackTrace(); } } for(int i = 0; i < deque.length; i++) { String temp = (String) deque[i]; System.out.println(temp); } in.close(); }
private void getChoice() throws EmptyDequeException { Scanner in = new Scanner(System.in); int temp = 0; do { System.out.println("What would you like to do?"); System.out.println("1: remove first"); System.out.println("2: remove last"); System.out.println("3. look first"); System.out.println("4. look last"); System.out.println("5. exit"); temp = in.nextInt(); switch(temp) { case 1: getFirst(); break; case 2: getLast(); break; case 3: System.out.println("First " + lookFirst()); break; case 4: System.out.println("Last " + lookLast()); break; case 5: System.out.println("Bye:"); break; } }while(temp !=5); } public static void main(String[] args) {
MyDeque dq = new MyDeque(10); dq.test(); } }
getFirst and getLast remove either first or last from the deque and then return the value. Or at least that is what they are supposed to do. In addition, the array capacity should change according to the size of deque. However, it should never drop below the default capacity of 10.
The test() and getChoice() are only for testing purposes and will eventually be changed.
On my latest test I started by adding 1 to the front.
I then added 2 to the front. Next, I added 10 to the back, then 9 to the back.
I then chose to removeFirst then removeLast. When I exit it displays all the elements in deque, and I got the following:
10 9 null null null null null null 2 1
My main question is this, if I removed first and last why are they still appearing in the deque when exiting? Am I doing something wrong in my methods to remove? I haven't done Java in a long time and my first time dealing with deques so apologies for rookie errors.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started