Защо това премахва всички елементи от моя LinkedList?

Защо моят метод за премахване премахва всеки елемент от моя двойно свързан списък? Ако извадя операторите if/else, тогава мога успешно да премахна средните елементи, но елементите в началото или опашката на списъка все още остават. Въпреки това добавих инструкциите if/else, за да се погрижа за елементите в началото и опашката, но за съжаление този метод вече премахва всеки елемент в моя списък. Какво правя грешно?

package week6;

import java.util.Iterator;

public class DblLinkedList<E>
{
   private LinkEntry<E> head = null;
   private LinkEntry<E> tail = null;
   private int size = 0;

   public DblLinkedList()
   {
      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;
   }

   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;
   }

   public void remove(int n)
   {
       LinkEntry<E> remove_this = new LinkEntry<E>();

       //if nothing comes before remove_this, set the head to equal the element after remove_this
       if (remove_this.previous == null)
           head = remove_this.next;

       //if nothing comes after remove_this, set the tail equal to the element before remove_this
       else if (remove_this.next == null)
           tail = remove_this.previous;
       //otherwise set the next element's previous pointer to the element before remove_this
       else
       {
           //if remove_this is located in the middle of the list, enter this loop until it is
           //found, then remove it, closing the gap afterwards.
           int i = 0;
           for (remove_this = head; remove_this != null; remove_this = remove_this.next)
           {
               //if i == n, stop and delete 'remove_this' from the list
               if (i == n)
               {               
                   //set the previous element's next to the element that comes after remove_this
                   remove_this.previous.next = remove_this.next;
                   //set the element after remove_this' previous pointer to the element before remove_this
                   remove_this.next.previous = remove_this.previous;
                   break;
               }
               //if i != n, keep iterating through the list
               i++; 
           }
       }
   }

   /*
    * 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 DblLinkedListImplIterate<E> implements Iterator<E>
   {

       protected LinkEntry<E> next;

       protected DblLinkedListImplIterate()
       {
           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
    }   
   }
}

И основният ми клас, в който тествам методите си, е:

package week6;

public class App {

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

        DblLinkedList<String> list = new DblLinkedList<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();
        System.out.println();

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

        //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(1);
        list.print_from_beginning();
        System.out.println();

        //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(5);
        list.print_from_end();
        System.out.println();

        //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();

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

    }
}

Резултатът, който се отпечатва, след като стартирам програмата, е:

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:


Remove Lisa and print the linked list starting from end:
Lisa Javier Krishna James Rohan Bill


Remove Krishna and print the linked list starting from the beginning:


List size: 0

person Brian    schedule 07.10.2012    source източник


Отговори (1)


Като добавите два блока if за обработка на първия и последния, вие сте добавили още 2 изпълними блока, които да се изпълняват в допълнение към това, което беше преди. Сега и трите блока се изпълняват заедно. Вярвам, че се опитвате да направите нещо подобно (изпълнете само един блок наведнъж):

   public void remove(int n)
   {
       LinkEntry<E> remove_this = new LinkEntry<E>();
      //if remove_this is located in the middle of the list, enter this loop until it is
       //found, then remove it, closing the gap afterwards.
       int i = 0;
       boolean removed = false;
       remove_this = head;

       while(removed == false){
           //if nothing comes before remove_this, set the head to equal the element after remove_this
           if (remove_this.previous == null){
               head = remove_this.next;
               head.previous = null;
               removed = true;
           }

           //if nothing comes after remove_this, set the tail equal to the element before remove_this
           else if (remove_this.next == null){
               tail = remove_this.previous;
               tail.next = null;
               removed = true;
           }
           //otherwise set the next element's previous pointer to the element before remove_this
           else{
               //if i == n, stop and delete 'remove_this' from the list
               if (i == n) {               
                   //set the previous element's next to the element that comes after remove_this
                   remove_this.previous.next = remove_this.next;
                   //set the element after remove_this' previous pointer to the element before remove_this
                   remove_this.next.previous = remove_this.previous;
                   removed = true;
                   break;
               }
               //if i != n, keep iterating through the list
           }
           if(!removed){
               remove_this = remove_this.next;
           }
           i++; 
       }
   }

Моля, обърнете внимание: Когато присвоявате head или tail в първите if else условия, по това време remove_this е празно и следователно превръщането на вашите head или tail в null и вашата програма за печат не отпечатва нищо.

person Yogendra Singh    schedule 07.10.2012
comment
Това наистина има по-логичен смисъл, но току-що го опитах и ​​все още премахва всички елементи. - person Brian; 07.10.2012
comment
Ето, актуализирах публикацията си, така че да включва целия клас, както и основния ми клас, в който тествам методите си. - person Brian; 07.10.2012
comment
Когато присвоявате heaad или tail в първите условия, по това време remove_this е празно и следователно прави вашите head или tail нулеви и вашата програма за печат не печата нищо. Моля, намерете моя актуализиран отговор с корекцията. - person Yogendra Singh; 07.10.2012
comment
РАБОТИ! Много благодаря! Работя по този метод през последните 3 дни. Оценявам помощта. - person Brian; 07.10.2012
comment
Току-що забелязах, че има един проблем с премахнат елемент, който се връща и след това отново изчезва, което аз попитано тук. Не мога да разбера дали компютърът ми просто се държи нестабилно или какво. - person Brian; 07.10.2012
comment
Дадох отговор в другия ти пост. Всъщност „head.previous = null;“ липсва от първата секция if в метода за премахване. Опитайте това и ми кажете. - person Yogendra Singh; 07.10.2012