为任何类型的链表类编写remove()方法

时间:2015-03-08 22:19:53

标签: java linked-list

我从头开始为链接列表类编写remove()方法时遇到一些问题。

有两个remove()方法,一个用于简单地删除链表的前面,另一个用于删除链表的特定索引处的数据。

我不确定哪一个被破坏了,我想我已经两个都正确但是通过运行我的另一个测试文件LinkedListTest,我收到有关remove()方法的错误消息:

Running Test remove tests (6 tests)
Starting tests: ....EE
Time: 0.004

There were 2 failures:
1) t44RemoveFromThreeItemList1(LinkedListTest$RemoveTest)
java.lang.AssertionError: Wrong item removed from One ItemList expected:<y> but was:<x>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotEquals(Assert.java:743)
        at org.junit.Assert.assertEquals(Assert.java:118)
        ... 11 more
2) t45RemoveFromThreeItemList2(LinkedListTest$RemoveTest)
java.lang.AssertionError: Wrong item removed from One ItemList expected:<z> but was:<y>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotEquals(Assert.java:743)
        at org.junit.Assert.assertEquals(Assert.java:118)
        ... 11 more

Test Failed! (2 of 6 tests failed.)

Test failures: abandoning other phases.

任何建议都表示赞赏,我已经看了很长时间,需要一些新鲜的眼睛。这是我的代码:

import java.util.*;

public class LinkedList<E> {
   private class ListNode {
      E data;
      ListNode next;

      ListNode(E data) {
         this.data = data;
         next = null;
      }

      ListNode(E data, ListNode next) {
         this.data = data;
         this.next = next;
      }
   }


   private ListNode front;

   private int size;

   public LinkedList() {
      front = null;


   }  
   public int size() {
      return size;
   }
   public boolean isEmpty() {
      if (this.size == 0) {
         return true;
      } else {
         return false;
      }
   }
   public void add(E value) {
      this.size++;
      if (front == null) {
         front = new ListNode(value);
      } else {
         ListNode current = front;
         while (current.next != null) {
            current = current.next;
         }
         current.next = new ListNode(value);
      }
   }

   public void add(int index, E value) {
       if (index == 0){
             front = new ListNode(value, front);
         }  
       else {
              ListNode curr = front;
              for   (int i =    0;  i < index - 1;  i++){
                    curr = curr.next;
              }
              curr.next = new   ListNode(value, curr.next);
         }
    }


   public E get(int index) {
      ListNode current = front;

      for (int i = 0; i < this.size(); i++, current = current.next) {
         if (i == index) {
            return current.data;
         }

      }
      return current.data;

   }
   public E remove() {
      ListNode result = front;
      front = front.next;
      size--;
      return result.data;


   }


   public E remove(int index) {
      ListNode result;
      if (index < 0 || index >= this.size()) {
         throw new IndexOutOfBoundsException();

      }
      if    (index == 0){
          result = front;
             front =    front.next;
          size--;
          return result.data;

        } else {
             ListNode curr = front;
             for (int i = 1; i <    index; i++){
                curr = curr.next;
             }

          result = curr;
             curr.next = curr.next.next;

        }
      size--;
      return result.data;


    }

此外,这里是测试文件的相关部分,用于上下文:

  public static class RemoveTest {   

      // Maximum 10 milliseconds for all tests
      @Rule public Timeout timeout = new Timeout(10);

      LinkedList<Character> oneList = null;
      LinkedList<Character> threeList = null;

      @Before public void setUp() {
         oneList = new LinkedList<Character>();
         oneList.add('a');
         threeList = new LinkedList<Character>();
         threeList.add('x');
         threeList.add('y');
         threeList.add('z');
      }

      @Test public void t41RemoveFromOneItemList() {
         assertEquals("Wrong item removed from One ItemList",
            new Character('a'), oneList.remove());
         assertEquals("zero element list is not empty.",
            true, oneList.isEmpty());
         assertEquals("zero element list has wrong size.",
            0, oneList.size());
      }

      @Test public void t42RemoveFromOneItemList() {
         assertEquals("Wrong item removed from One ItemList",
            new Character('a'), oneList.remove(0));
         assertEquals("zero element list is not empty.",
            true, oneList.isEmpty());
         assertEquals("zero element list has wrong size.",
            0, oneList.size());
      }

      @Test public void t42RemoveFromThreeItemList() {
         assertEquals("Wrong item removed from One ItemList",
            new Character('x'), threeList.remove());
         assertEquals("two element list is empty.",
            false, threeList.isEmpty());
         assertEquals("two element list has wrong size.",
            2, threeList.size());
         assertEquals("get(0) on two element list is wrong.",
            new Character('y'), threeList.get(0));
      }

      @Test public void t43RemoveFromThreeItemList0() {
         assertEquals("Wrong item removed from One ItemList",
            new Character('x'), threeList.remove(0));
         assertEquals("two element list is empty.",
            false, threeList.isEmpty());
         assertEquals("two element list has wrong size.",
            2, threeList.size());
         assertEquals("get(0) on two element list is wrong.",
            new Character('y'), threeList.get(0));
      }

      @Test public void t44RemoveFromThreeItemList1() {
         assertEquals("Wrong item removed from One ItemList",
            new Character('y'), threeList.remove(1));
         assertEquals("two element list is empty.",
            false, threeList.isEmpty());
         assertEquals("two element list has wrong size.",
            2, threeList.size());
         assertEquals("get(0) on two element list is wrong.",
            new Character('x'), threeList.get(0));
         assertEquals("get(1) on two element list is wrong.",
            new Character('z'), threeList.get(1));
      }

      @Test public void t45RemoveFromThreeItemList2() {
         assertEquals("Wrong item removed from One ItemList",
            new Character('z'), threeList.remove(2));
         assertEquals("two element list is empty.",
            false, threeList.isEmpty());
         assertEquals("two element list has wrong size.",
            2, threeList.size());
         assertEquals("get(1) on two element list is wrong.",
            new Character('y'), threeList.get(1));
      }
   }

1 个答案:

答案 0 :(得分:0)

您的remove(index)方法出错。在循环中,如果要使用index = 1开始迭代,则必须使用curr初始化front.next,否则您将错过一个步骤。恕我直言,这样做更好:

public E remove(int index) {
    ListNode result;
    if (index < 0 || index >= this.size()) {
        throw new IndexOutOfBoundsException();

    }
    ListNode curr = front;
    for (int i = 0; i < index; i++){
       curr = curr.next;
    }

    // front case added
    if(index == 0){
        front = curr.next;
    }

    result = curr;
    curr.next = curr.next.next;

    size--;
    return result.data;
}