【问题标题】:Copy Constructor Assertion Error复制构造函数断言错误
【发布时间】:2017-08-18 02:03:39
【问题描述】:

我的复制构造函数由于断言错误而失败。据说队列的大小不正确,我不知道为什么。这是我的 Queue 类的代码:

public class Queue<T> implements UnboundedQueueInterface<T> {

    public Node<T> head;
    public Node<T> tail;
    public int size = 0;

    public Queue() {        
            // TODO 1

    }

    public Queue(Queue<T> other) {
            // TODO 2 
        if(other.head==null){
            this.head=null;}
        else{
        Node<T> newN = new Node<T>(other.head.data);
        newN = other.head;
        while(newN!=null){
            T element = newN.data;
            this.enqueue(element);
            newN = newN.next;}
        }

    }

    @Override
    public boolean isEmpty() {
            // TODO 3
      return head==null;
    }

    @Override
    public int size() {
            // TODO 4
            int sum = 0;
            while(head!=null){
                sum+=1;
                head = head.next;
            }
            return sum;

    }

    @Override
    public void enqueue(T element) {
            // TODO 5
        Node<T> newNode = new Node<T>(element, null);
        if (isEmpty()) {head = newNode;} else {tail.next = newNode;}
        tail = newNode;
        this.size++;

    }

    @Override
    public T dequeue() throws NoSuchElementException {
            // TODO 6
            if(isEmpty()){ 
                throw new NoSuchElementException("empty queue");}
            else{
                T element = head.data;
                if (tail == head) {
                    tail = null;
                }
                head = head.next;
                this.size--;
                return element;}

    }

    @Override
    public T peek() throws NoSuchElementException {
            // TODO 7
           if(isEmpty()) throw new NoSuchElementException("empty queue");
           else
               return head.data;
    }


    @Override
    public UnboundedQueueInterface<T> reversed() {
            // TODO 8

            Queue<T> output = new Queue<T>(this);
            Node<T> node1 = new Node<T>(output.head.data);
            node1 = output.head;
            Node<T> node2 = new Node<T>(null);  //nextNode
            Node<T> node3 = new Node<T>(null);  //prevNode

            while(node1!=null){
                node2 = node1.next;
                node1.next = node3;
                node3 = node1;
                node1 = node2;
            }
            output.head = node3;
            return output;

            }



}

class Node<T> {
    public T data;
    public Node<T> next;
    public Node(T data) { this.data=data;}
    public Node(T data, Node<T> next) {
        this.data = data; this.next=next;
    }
}

下面是测试代码:

public void testCopyConstructorEmptyNotAliased() throws Exception  {
        Queue<Integer> q = new Queue<Integer>();
        UnboundedQueueInterface<Integer> r;
        r = new Queue<Integer>(q);
        assertTrue(r.isEmpty());
        assertTrue(q.isEmpty());        

        q.enqueue(1);
        q.enqueue(2);
        assertEquals(2, q.size());
        assertTrue(r.isEmpty());

        r.enqueue(3);
        r.enqueue(4);
        r.enqueue(5);
        assertEquals(2, q.size());
        assertEquals(3, r.size());

        r.dequeue();
        r.dequeue();
        r.dequeue();
        assertTrue(r.isEmpty());
        assertEquals(2, q.size());

        q.dequeue();
        q.dequeue();
        assertTrue(q.isEmpty());
    }

【问题讨论】:

  • AssertionError发生在哪一行,队列的长度是多少(会在失败信息中说明)
  • 啊好吧我知道错误来自哪里。您的public int size() 方法实际上修改了成员head,因此在您再次检查大小后,头部位于最后一个元素之后(即null)并且长度为0。您必须在您的大小内创建一个临时变量功能
  • 谢谢!我什至忘了考虑!

标签: java linked-list queue copy-constructor assertion


【解决方案1】:

您甚至不需要它遍历整个队列来查找队列的大小。当执行任何入队或出队操作时,您已经在更新队列大小。您可以简单地返回大小。

 @Override
    public int size() {
            // TODO 4
        return this.size;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2013-12-30
    • 2015-08-02
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    相关资源
    最近更新 更多