Java双向链接列表removeLast()

时间:2017-07-13 19:01:00

标签: java

我有一个类赋值,要求我创建一个实现ListADT的类DoubleList。

我已经达到代码ALMOST正常工作的程度。代码的输出应该是: 1 3 7 9 13 14 16 17 23 24
3 9 13 16

我的输出是: 1 3 7 9 13 14 16 17 23 24
3 9 13 16 23

第一个removeLast()似乎删除了24但是由于某种原因,在调用第二个removeLast()之后23个停留。请帮忙!

编辑:如果我再次调用removeLast(),它将删除23。

class DoubleList<T> implements ListADT<T>{

    private int _size;
    private DoubleNode _head;
    private DoubleNode _tail;

    public DoubleList() {
        _size = 0;
        _head = null;
        _tail = null;
    }

    public T removeFirst(){
        if(_size == 0){
            return null;
        }
        DoubleNode tmp = _head;
        _head = _head._next;
        _head._previous = null;
        _size--;
        return tmp._value;
    }

    public T removeLast(){
        if(_size == 0) {
            return null;
        }
        T temp = _tail._value;
        _tail = _tail._previous;
        _tail._next = null;
        _size--;
        return temp;
    }

    public T remove(T element){
        if(_size == 0){
            return null;
        }

        DoubleNode current = _head;
        DoubleNode previous = null;
        T temp = null;

        do{
            if(current._value == element){
                temp = current._value;
                if(previous == null){
                    _head = _head._next;
                    _head._previous = null;
                }
                else{
                    previous._next = current._next;
                }
            }
            previous = current;
            current = current._next;
        }while(current != null);
        return temp;
    }

    public T first(){
        return _head._value;
    }

    public T last(){
        return _tail._value;
    }

    public boolean contains(T target){
        if(_size == 0){
            return false;
        }

        DoubleNode temp = _head;

        do{
            if(temp._value == target){
                return true;
            }
            temp = temp._next;
        }while(temp != null);
        return false;
    }

    public boolean isEmpty(){
        if(_size == 0){
            return true;
        }
        return false;
    }

    public int size(){
        return _size;
    }

    public void add(T element)
    {
        int add = 0;
        DoubleNode temp = new DoubleNode();
        temp._value = element;
        DoubleNode point = _head;
        DoubleNode placeHolder;

        if(_head == null) {
            _head = temp;
            _tail = temp;
            _size++;
            return;
        }
        else if((Integer)element <= (Integer)_head._value){
            temp._next = _head;
            _head._previous = temp;
            _head = temp;
            _size++;
            return;
        }

        do {

            if(point._next == null){
                point._next = temp;
                temp._previous = point;
                _tail = temp;
                _size++;
                return;
            }
            else if((Integer)point._next._value >= (Integer)element && (Integer)point._value < (Integer)element){
                placeHolder = point._next;
                point._next = temp;
                placeHolder._previous = temp;
                temp._next = placeHolder;
                temp._previous = point;
                _size++;
                return;
            }

            point = point._next;

        } while (point != null);


        _size++;
    }

    public String toString(){
        String returnString = "";
        if(_size == 0){
            return returnString;
        }

        DoubleNode temp = _head;

        do{
            returnString += temp._value + " ";
            temp = temp._next;
        }while(temp != null);
        return returnString;
    }



    private class DoubleNode {
        private DoubleNode _previous;
        private DoubleNode _next;
        private T _value;

        public DoubleNode() {
            _previous = null;
            _next = null;
            _value = null;
        }

        public DoubleNode(T value){
            _previous = null;
            _next = null;
            _value = value;
        }

    }

}

/**
 * DoubleOrderedList testing area.
 *
 * @author (your name), Acuna
 * @version (version)
 */
class Driver {
    public static void main(String [] args) {
        DoubleList<Integer> list = new DoubleList<>();

        //RA: These are _extremely_ simple tests - do not use them when doing
        //    your writeup.

        list.add(23);
        list.add(24);
        list.add(16);
        list.add(3);
        list.add(7);
        list.add(17);
        list.add(9);
        list.add(13);
        list.add(14);
        list.add(1);
        System.out.println("\nsize = " + list.size());

        System.out.println(list);

        list.remove(7);
        System.out.println(list);
        list.removeFirst();
        System.out.println(list);
        list.remove(17);
        System.out.println(list);
        list.removeLast();
        System.out.println(list);
        list.remove(14);
        System.out.println(list);
        list.removeLast();
        System.out.println(list);

2 个答案:

答案 0 :(得分:1)

我认为您的错误不在removeLast。它位于remove函数中,它会破坏您的列表,导致对列表的进一步操作行为不正确。

当您从双链表的中间删除项目时,您需要在删除的项目之前拼接项目,删除项目后的项目。您正在进行第一次操作,但不是第二次操作。例如,假设我在列表中有三个元素:LX R.我想删除X.我必须设置L._next = R(你这样做),并设置R._previous = L(你不做那)。此时,您的列表已损坏,因为您的反向链接已关闭。

答案 1 :(得分:0)

在删除功能时查看你的循环。

do{
            if(current._value == element){
                temp = current._value;
                if(previous == null){
                    _head = _head._next;
                    _head._previous = null;
                }
                else{
                    previous._next = current._next;
                    //next line is missing
                    previous._next._previous = previous;
                }
            }
            previous = current;
            current = current._next;
        }while(current != null);

此外,循环时效率不高(current!= null),考虑(while!found&amp;&amp; current!= null)或break语句。