Problems with implementing a deque in java

别来无恙 提交于 2019-12-02 08:23:01

The big glaring issue is that end never changes. isEmpty() will always return true. Now let's look at your insertFirst() method.

public void insertFirst(EltType first) {
    if(!isEmpty()) {
        EltType[] tempArray;
        tempArray = (EltType[]) new Object[capacity+1];
        for (int i=0;i<deque.length;i++) {
            tempArray[i+1] = deque[i]; 
        }

        deque = tempArray; 
    } 
    deque[0] = first;
}

Knowing that isEmpty() always returns true no matter what, what is the problem with this piece of code?

You need to update your end pointer too when you remove an element.

You should also investigate System.arrayCopy()

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!