Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3953312
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:57:36+00:00 2026-05-20T01:57:36+00:00

Been working on this for a while now and I think I’ve finally cracked

  • 0

Been working on this for a while now and I think I’ve finally cracked it, it’s working for all my tests, but I have a feeling there will be some niggling issues. This is a heavily simplified version of a double sided queue (deque) where every time a value is added, a temporary array is made to store all values, and then the new value appended on. It is easiest to explain this way, I believe. If someone could please just double-check I am correct and there is nothing glaringly wrong here, I would be extremely thankful. Thank you all very much ! 🙂

    public class ArrayBasedDeque<EltType> implements Deque<EltType> {

  private final int CAPACITY = 10;
  private int capacity;
  private int end;
  private EltType deque[];  

  public ArrayBasedDeque() {
    this.capacity = CAPACITY;
    deque = (EltType[]) (new Object[capacity]);  
  }
  public EltType first() {
    return  deque[0];
  }
  public EltType last() {
    return deque[end-1];
  }

  public boolean isEmpty() {
    return end == 0;
  }

  public int size() {
   return deque.length;
  }

  public boolean isFull() {
   return end == capacity;
  }
  public void insertFirst(EltType inserted) {
    if (!isEmpty()) {
    EltType[] tempArray;
    capacity+=1;
    tempArray = (EltType[]) new Object[capacity];
    for(int i=0;i<end;i++){
    tempArray[i+1] = deque[i]; 
    }
    deque=tempArray;
    }
    deque[0] = inserted;
    end++;
    }

  public void insertLast(EltType last) {
    if (isFull()){
          EltType[] tempArray;
          capacity+=1;
      tempArray = (EltType[]) new Object[capacity];
      for (int i=0;i<end;i++) {
        tempArray[i] = deque[i]; 
      }
//      System.out.print(deque[end]);
    }
    deque[end] = last;   
    end++;
  }

  public EltType removeFirst() {
    EltType[] tempArray;
    EltType returned = deque[0];
    tempArray = (EltType[]) new Object[capacity];
      for (int i=1;i<capacity;i++) {
        tempArray[i-1] = deque[i]; 
      }
      deque = tempArray;
      end--;
    return returned;
  }

  public EltType removeLast() {
    EltType[] tempArray;
    EltType returned = deque[end-1];

    tempArray = (EltType[]) new Object[capacity];
      for (int i=0;i<capacity;i++) {
        tempArray[i] = deque[i]; 
      }
      deque = tempArray;
      end--;
    return returned;
  }




}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:57:37+00:00Added an answer on May 20, 2026 at 1:57 am

    A few comments:

    • I would use T or E as the name of the type parameter, rather than EltType
    • I’d rename the constant CAPACITY to DEFAULT_CAPACITY, and make it static.
    • first() will return a value even if the deque is logically empty
    • last(), removeLast() and removeFirst() should throw an appropriate exception if end is 0
    • There’s no point in having a capacity separate from the size unless you’re using that to avoid creating a new array each time. If you’re always going to expand/shrink the array on any change, just use the array on its own – you can tell the size just from the array’s length
    • In removeFirst and removeLast your loop bound is capacity instead of end
    • Use System.arraycopy as a simpler way to copy arrays
    • You haven’t got an assignment to deque in insertLast – hence the exception you’re seeing in the comments.

    I’m not sure I see the benefit of having this over just using ArrayList<T> though… the main point of having a separate Deque implementation would be to make adding to both head and tail cheap… here we have neither!

    … or of course just use ArrayDeque or LinkedList 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been pondering this for a while but cannot come up with a working
I've been working on this for a while now and I cant seem to
I've been working out this query now for a while and I thought I
Been searching for the answer to this for a while now and I think
I've been working on this for a few days now, and I've found several
I have been working on this for the greater part of the day and
I have this classic ASP site which has been working fine until we updated
I have been working on Flex for last couple of months and as this
I've been trying this a couple of different ways, but it's not working for
I've been working on a GUI program for a while now, and I just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.