在用户定义的Java Doubly LinkedList中添加和删除元素

时间:2012-10-06 03:32:19

标签: java linked-list

我正在为下周的考试而学习,我希望在添加和删除双向链接列表中的元素时,有人可以帮助我。这个逻辑对我来说很有意义,不幸的是我在编码方面还不是很好(只有我的第二个计算机科学课程)。我已经浏览了整个网络和我的文本,但我似乎无法找到一个足够接近我的问题的例子。下面我将发布我的教授要求我完成的内容,以及我目前的代码。提前感谢您的帮助。

更新

我在添加时遇到了问题,但是有人能够帮助我,但是我的删除方法仍然存在问题。我觉得奇怪的是我的教授有一个除了void之外的返回类型的remove方法。有人可以解释一下吗?无论如何,我的更新代码如下。

来自教授:

将CS401DblLinkedListImpl.java中缺少的代码填入 在课堂上讲了。

要测试您的代码,请创建一个双向链接列表并输入 其中包含String类型的元素:

比尔,罗汉,詹姆斯,克里希纳,哈维尔,丽莎

(a)从头开始打印链表。

(b)从最后开始打印链表。

(c)删除账单并从头开始打印链表。

(d)删除Lisa并从头开始打印链表。

(e)删除Krishna并从中打印链接列表 开始。

以下代码集是我的用户定义类,我将测试的方法是boolean add(E e),E remove(int n),void print_from_beginning()和void print_from_end():

package week6;

import java.util.Iterator;

public class CS401DblLinkedListImpl<E> //implements CS401CollectionInterface<E> 
{
   private LinkEntry<E> head = null;
   private LinkEntry<E> tail = null;
   private int size = 0;

   public CS401DblLinkedListImpl()
   {
      head = tail = null;
   }

   public boolean is_empty()
   {
      if (head == null) 
          return true;

      return false;
   }

   public int size()
   {
       int count = 0;
       for (LinkEntry<E> current = head; current != null; current = current.next)
           count++;
       return count;
   }

   /*
    * Add e to the end of the doubly linked list.
    * Returns true - if e was successfully added, false otherwise.
    */
   public boolean add(E e)
   {   
      LinkEntry<E> new_element = new LinkEntry<E>();
      new_element.element = e;

          if (head == null)
          {
          new_element.next = head;
          head = new_element;
          tail = head;
      }
      else
      {
          tail.next = new_element;
          new_element.previous = tail;
          tail = new_element;
      }
      return true;
   }

   /*
    * Remove the nth element in the list.  The first element is element 1.
    * Return the removed element to the caller.
    */
   public E remove(int n)
   {
      LinkEntry<E> current = new LinkEntry<E>();
      int i = 0;

      while (n == i++)
      {
          current.previous.next = current.next;
          if (current.next == null)
          {
              current.next.previous = current.previous;
          }
      }
      return (E) current;
   }

  /*
   * Print the doubly linked list starting at the beginning.
   */
   public void print_from_beginning()
   {
      LinkEntry<E> current = new LinkEntry<E>();
      for (current = head; current != null; current = current.next)
      {
          System.out.print(current.element + " ");
      }
   }

   /*
    * Print the doubly linked list starting the end.
    */
   public void print_from_end()
   {
      LinkEntry<E> current = new LinkEntry<E>();
      for (current = tail; current != null; current = current.previous)
      {
          System.out.print(current.element + " ");
      }
   }

   /* ------------------------------------------------------------------- */
   /* Inner classes                                                      */
   protected class LinkEntry<E>
   {
      protected E element;
      protected LinkEntry<E> next;
      protected LinkEntry<E> previous;

      protected LinkEntry() { element = null; next = previous = null; }
   }
   /* ------------------------------------------------------------------- */
   protected class CS401DblLinkedListImplIterate<E> implements Iterator<E>
   {

       protected LinkEntry<E> next;

       protected CS401DblLinkedListImplIterate()
       {
           next = (LinkEntry<E>) head;
       }

       @Override
       public boolean hasNext() {
           // TODO Auto-generated method stub
           return false;
       }

       @Override
       public E next() {
       // TODO Auto-generated method stub
       return null;
       }

       @Override
       public void remove() {
       // TODO Auto-generated method stub
       }
      }
} /* CS401LinkedListImpl<E> */

下面是我测试方法的主要课程:

package week6;

import java.util.*;

public class App {

    public static <E> void main(String[] args) {

    /*
     * Part 1 of lab 6: Testing CS401DblLinkedListImpl
     */
    System.out.println("Testing Lab 6: Part 1...");
    CS401DblLinkedListImpl<String> list = new CS401DblLinkedListImpl<String>();

    list.add("Bill");
    list.add("Rohan");
    list.add("James");
    list.add("Krishna");
    list.add("Javier");
    list.add("Lisa");

    System.out.println("List size after all names are added: " + list.size());

    //a. Print the linked list starting at the beginning.
    System.out.println("\nPrint the linked list starting at the beginning:");
    list.print_from_beginning();

    //b. Print the linked list starting at the end.
    System.out.println("\nPrint the linked list starting at the end:");
    list.print_from_end();

    //c. Remove Bill and print the linked list starting from beginning.
    System.out.println("\nRemove Bill and print the linked list starting from beginning:");
    list.remove(0);
    list.print_from_beginning();

    //d. Remove Lisa and print the linked list starting from end.
    System.out.println("\nRemove Lisa and print the linked list starting from end:");
    list.remove(4);
    list.print_from_end();

    //e. Remove Krishna and print the linked list starting from the beginning.
    System.out.println("\nRemove Krishna and print the linked list starting from the beginning:");
    list.remove(2);
    list.print_from_beginning();

    System.out.println("\nList size: " + list.size());
    }
}

最后,下面是我运行程序时得到的结果:

Testing Lab 6: Part 1...
List size after all names are added: 6

Print the linked list starting at the beginning:
Bill Rohan James Krishna Javier Lisa 
Print the linked list starting at the end:
Lisa Javier Krishna James Rohan Bill 
Remove Bill and print the linked list starting from beginning:
Exception in thread "main" java.lang.NullPointerException
    at week6.CS401DblLinkedListImpl.remove(CS401DblLinkedListImpl.java:67)
    at week6.App.main(App.java:34)

仅供参考,CS40​​1DblLinkedListImpl.java:67在我的remove方法中引用以下代码行:

      current.previous.next = current.next;

对此的任何帮助将不胜感激。我觉得我有点接近答案,只需要澄清一下。感谢。

1 个答案:

答案 0 :(得分:1)

您当前的代码无法处理删除索引0。

LinkEntry<E> current = head;
if (n == 0) {
    // something like this maybe...
    head = head.next;
    if (head != null) { head.previous = null; }
    return current;
}

你从i = 0开始,但是做一个i ++,所以你处于领先地位,但索引为1。

你的删除逻辑对我来说也很奇怪。

 current.previous.next == current (is true)
 current.next.previous == current (is true)

所以它应该是这样的:

if (n == i++) {
    // We're at the spot so let's remove it.
    current.previous.next = current.next;
    if (current.next != null) { current.next.previous = current.previous; }
    return current;
}

另外我相信你添加逻辑也是错误的,应该是这样。

LinkEntry<E> new_element = new LinkEntry<E>();
new_element.element = e;
if (head == null) {
    head = new_element;
    tail = head;
} else {
    tail.next = new_element;
    new_element.previous = tail;
    tail = new_element;
}

size++;  // You don't use size it looks like...  and your size starts at 1 which is 
         // wrong, it should start at 0 since it's empty, also remove would have to
         // update size.
return true;