Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 复制构造函数断言错误_Java_Linked List_Queue_Copy Constructor_Assertion - Fatal编程技术网

Java 复制构造函数断言错误

Java 复制构造函数断言错误,java,linked-list,queue,copy-constructor,assertion,Java,Linked List,Queue,Copy Constructor,Assertion,由于断言错误,我的复制构造函数失败。这是说队列的大小不正确,我不知道为什么。以下是我的队列类的代码: public class Queue<T> implements UnboundedQueueInterface<T> { public Node<T> head; public Node<T> tail; public int size = 0; public 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;
    }
}
公共类队列实现了无界队列接口{
公共节点头;
公共节点尾部;
公共整数大小=0;
公共队列(){
//待办事项1
}
公共队列(其他队列){
//待办事项2
if(other.head==null){
this.head=null;}
否则{
Node newN=新节点(other.head.data);
newN=其他头;
while(newN!=null){
T元素=新数据;
这个。排队(元素);
newN=newN.next;}
}
}
@凌驾
公共布尔值为空(){
//待办事项3
返回头==null;
}
@凌驾
公共整数大小(){
//待办事项4
整数和=0;
while(head!=null){
总和+=1;
head=head.next;
}
回报金额;
}
@凌驾
公共无效排队(T元素){
//待办事项5
Node newNode=新节点(元素,空);
if(isEmpty()){head=newNode;}else{tail.next=newNode;}
tail=newNode;
这个.size++;
}
@凌驾
public T dequeue()抛出NoTouchElementException{
//待办事项6
如果(isEmpty()){
抛出新的NoSuchElementException(“空队列”);}
否则{
T元素=head.data;
如果(尾部==头部){
tail=null;
}
head=head.next;
这个尺寸--;
返回元素;}
}
@凌驾
public T peek()抛出NoTouchElementException{
//待办事项7
如果(isEmpty())抛出新的NosTouchElementException(“空队列”);
其他的
返回头数据;
}
@凌驾
公共无界QueueInterface reversed(){
//待办事项8
队列输出=新队列(此);
Node node1=新节点(output.head.data);
node1=output.head;
Node node2=新节点(null);//nextNode
Node node3=新节点(null);//prevNode
while(node1!=null){
node2=node1.next;
node1.next=node3;
节点3=节点1;
节点1=节点2;
}
output.head=node3;
返回输出;
}
}
类节点{
公共数据;
公共节点下一步;
公共节点(T data){this.data=data;}
公共节点(T数据,下一个节点){
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());
    }
public void testCopyConstructorEmptyNotAliased()引发异常{
队列q=新队列();
无界排队器;
r=新队列(q);
assertTrue(r.isEmpty());
assertTrue(q.isEmpty());
q、 排队(1);
q、 排队(2);
assertEquals(2,q.size());
assertTrue(r.isEmpty());
r、 排队(3);
r、 排队(4);
r、 排队(5);
assertEquals(2,q.size());
assertEquals(3,r.size());
r、 出列();
r、 出列();
r、 出列();
assertTrue(r.isEmpty());
assertEquals(2,q.size());
q、 出列();
q、 出列();
assertTrue(q.isEmpty());
}

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

@覆盖
公共整数大小(){
//待办事项4
返回此.size;
}

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

@覆盖
公共整数大小(){
//待办事项4
返回此.size;
}

AssertionError发生在哪一行,队列有多长(它会在失败消息中说出来)啊,好的,我知道错误来自哪里。您的
public int size()
方法实际上修改了成员
head
,因此再次检查大小后,head位于最后一个元素(即null)之后的元素上,长度为0。您必须在size函数中创建一个临时变量谢谢!我都忘了想那件事!AssertionError发生在哪一行,队列有多长(它会在失败消息中说)啊,好的,我知道错误来自哪里。您的
public int size()
方法实际上修改了成员
head
,因此再次检查大小后,head位于最后一个元素(即null)之后的元素上,长度为0。您必须在size函数中创建一个临时变量谢谢!我都忘了想那件事!