双头deque

时间:2018-01-23 21:35:16

标签: java data-structures deque

我正在制作一个双端双端队列。我收到错误:Deque<String> d = new Deque<String>();错误是Deque不接受参数。

这是我的Deque:

import com.sun.org.apache.xpath.internal.operations.String;
import java.util.Iterator;


public abstract class Deque implements DoubleEndedQueue<Object>,Iterator<Object> {

private static Node<Object> first;
private static Node<Object> last;

private static int size = 0;

public Deque() {
    size = 0;
    first = null;
    last = null;
}

private static class Node<Object> {

    Object item;
    Node<Object> prec;
    Node<Object> next;
}

DoubleEndedQueue<Object> d = new DoubleEndedQueue<Object>() {

        @Override
        public boolean isEmpty() {
            return size == 0;
        }

        @Override
        public int size() {
            return size;
         }

        @Override
        public void pushLeft(Object item) {
            Node<Object> newNode = new Node<Object>();
            newNode.item = item;
            if (size == 0) {
                first = last = newNode;
            } else {
                newNode.next = first;
                first.prec = newNode;
            }
            first = newNode;
            if (last == null) {
                last = first;
            }
            size++;
        }

        @Override
        public void pushRight(Object item) {
            Node<Object> newNode = new Node<Object>();
            newNode.item = item;

            if (size == 0) {
                last = first = newNode;
            } else {
                newNode.prec = last;
                last.next = newNode;
            }
            last = newNode;
            if (first == null) {
                first = last;
            }
            ++size;
        }

        @Override
        public Object popLeft() {
            Node<Object> newNode = new Node<Object>();
            newNode = first;
            first = first.next;

            if (first == null) {
                last = null;
            } else {
                first.prec = null;
            }

            size--;

            return newNode.item;
        }

        @Override
        public Object popRight() {
            Node<Object> newNode = new Node<Object>();
            newNode = last;
            last = newNode.prec;

            if (last == null) {
                first = null;
            } else {
                last.next = null;
            }

            size--;

            return newNode.item;
        }

        @Override
        public Object changeLeft(int n, Object newItem) {
            Node<Object> newNode = new Node<Object>();
            newNode = first;
            for (int i = 0; i < n; i++) {
                newNode = newNode.next;
            }
            return newNode.item = newItem;
        }

        @Override
        public Object changeRight(int n, Object newItem) {
            Node<Object> newNode = new Node<Object>();
            newNode = last;
            for (int i = 0; i < n; i++) {
                newNode = newNode.prec;
            }
            return newNode.item = newItem;
        }



    };

 public Iterator<Object> iterator() {
    return new Iterator<Object>() {

        private Node<Object> node = first;

        @Override
        public boolean hasNext() {
            return node != null;
        }

        @Override
        public Object next() {
            Object item = node.item;
            node = node.next;

            return item;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}

public interface Iterable<Object> {
   Iterable<Object> iterator();
}

public static void main(String[] args) {
    Deque<String> d = new Deque<String>();
    d.pushLeft("im first");
    d.pushLeft("im second");
    d.pushRight("im third");
    d.pushRight("im fourth");
    d.pushLeft("im fifth");
    d.pushRight("im sixth");
    d.changeLeft(1, "well");
    d.changeRight(2, "this");
    d.changeLeft(1, "was");
    d.changeRight(3, "fun");
    d.popRight();
    d.popLeft();


}

}

2 个答案:

答案 0 :(得分:0)

这里有几个问题:

  1. Dequeabstract,这意味着您无法使用new对其进行实例化。
  2. Deque课程中,您在new上调用DoubleEndedQueue,但看起来这是一个界面。您无法在界面上拨打new
  3. 您可以采用&#34; new DoubleEndedQueue&#34;中的重写方法。并直接将其放在Deque类中,并删除摘要。
  4. 希望这能指出你正确的方向。

答案 1 :(得分:0)

因为在创建对象时使用的是泛型,但在创建类时却没有。 您需要提供- 公共抽象类Deque实现DoubleEndedQueue,Iterator {}