简体   繁体   English

双链表无进口

[英]Doubly Linked List no imports

I'm currently working on Junit testing for implementing a Doubly Linked list in java. 我目前正在进行Junit测试,以在Java中实现双向链接列表。 I understand how it needs to work drawn pictures over and over but I cannot get my removeFirst method to work. 我了解如何反复处理绘制的图片,但是我无法使removeFirst方法正常工作。 addLast(); works up until removeFirst is called. 直到调用removeFirst为止。

private Link<I> first;
private Link<I> last;

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

public int size() {
    int count = 0;
    Link<I> aLink = first;
    while (aLink != null) {
        count++;
        aLink = aLink.getAfter();
    }
    return count;
}

public I get(int index) {
    Link<I> aLink = first;
    int count = 0;
    while (count != index) {
        aLink = aLink.getAfter();
        count++;
    }
    return aLink.getItem();

}

public void addLast(I anItem) {
    Link<I> aLink = new Link<I>(anItem);
    if (isEmpty()) {
        first = aLink;
        first = last;
    } else {
        last.setAfter(aLink);
        aLink.setBefore(last);
        last = aLink;

    }
}

public void addFirst(I anItem) {
    Link<I> aLink = new Link<I>(anItem);
    if (isEmpty()) {
        first = aLink;
        last = first;
    } else {
        aLink.setAfter(first);
        first.setBefore(last);
        first = aLink;
    }

}

public I removeFirst() {
    I removed = first.get(0);
    if(size()==3) {
        first = first.getAfter();
        first.setBefore(null);
        first.setAfter(last);
        return removed;
    } else if(size()==2) {
        first = first.getAfter();
        first.setBefore(null);
        first.setAfter(null);
        return removed;
    }else {
        first = null;
        first.setBefore(null);
        first.setAfter(null);
        return removed;
    }

}

public I removeLast() {
    I removed = last.getItem();
    if (isEmpty()) {
        removed = null;
        return removed;
    } else {
        last = last.getBefore();
        removed = last.getItem();
        return removed;
    }

}

} }

public class Link<I> {
private Link<I> after;
private Link<I> before;
private I item;

public Link(I anItem) {
    item = anItem;
}

public Link<I> getAfter(){
    return after;
}

public void setAfter(Link<I> aLink) {
    after = aLink;
}

public Link<I> getBefore(){
    return before;
}

public void setBefore(Link<I> aLink) {
    before = aLink;
}

public I getItem() {
    return item;
}

public void setItem(I anItem) {
    item = anItem;
}

} }

here is the test unit 这是测试单位

void testAddLast() {
    notes.addLast("do");
    notes.addLast("re");
    notes.addLast("mi");
    String note = notes.removeFirst();
    assertTrue(notes.size()==2);
    assertTrue("do".equals(note));
    note = notes.removeFirst();
    assertTrue(notes.size()==1);
    assertTrue("re".equals(note));
    note= notes.removeFirst();
    assertTrue(notes.isEmpty());
    assertTrue("mi".equals(note));
    note = notes.removeFirst();
    assertTrue(note == null);
    assertTrue(notes.isEmpty());
    assertTrue(notes.size() == 0);
}

I feel like what I have now is getting me close but I removed = first.get(0); 我觉得我现在正在靠近我,但是I removed = first.get(0); keeps giving me a null pointer exception. 一直给我一个空指针异常。 I was given extra time on this assignment over 2 weeks ago and I am still having a really hard time with this. 在2个星期前,我被分配了更多的时间来做这项作业,但我仍然很难过。 I have tried a bunch of variations of the removeFirst() method following the debugger to help. 我在调试器之后尝试了removeFirst()方法的多种变体来提供帮助。

any insight would help me alot. 任何见识都会对我有很大帮助。 I have more test cases I can add to post aswell. 我还有更多测试用例可以添加到帖子中。

This seems to be the method that's giving you trouble 这似乎是给您带来麻烦的方法

public I removeFirst() {
    I removed = first.get(0);
    if(size()==3) {
        first = first.getAfter();
        first.setBefore(null);
        first.setAfter(last);
        return removed;
    } else if(size()==2) {
        first = first.getAfter();
        first.setBefore(null);
        first.setAfter(null);
        return removed;
    }else {
        first = null;
        first.setBefore(null);
        first.setAfter(null);
        return removed;
    }

}

You say you're getting a NullPointerException when you call first.get(0) . 您说在调用first.get(0)时收到NullPointerException。 This means that first is null. 这意味着first为null。

This is how you add a node: 这是添加节点的方式:

public void addLast(I anItem) {
    Link<I> aLink = new Link<I>(anItem);
    if (isEmpty()) {
        first = aLink;
        first = last;
    } else {
        last.setAfter(aLink);
        aLink.setBefore(last);
        last = aLink;
    }
}

You assign to first aLink , then overwrite that assignment with last , which is null. 您将分配给first aLink ,然后用last覆盖该分配,该值为null。

You probably means to say last = first there. 您可能想说的是last = first

Try this test case: 试试这个测试用例:

void testAddOne() {
    assertTrue(notes.count() == 0);
    notes.addLast("do");
    assertTrue(notes.count() == 1);
}

Generally, your test case is very complicated. 通常,您的测试用例非常复杂。 You want simpler tests to check the postconditions of an operations. 您希望使用更简单的测试来检查操作的后置条件。 For example, inserting one element should increase the count by one. 例如,插入一个元素应使计数增加一。 Removing an element should decrease the count by one. 删除元素应将计数减少一。 Adding two elements, check get returns the first for index 0 and the second for index 1, and so on. 添加两个元素,check get返回第一个索引0,第二个索引1,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM