Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8639599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:03:17+00:00 2026-06-12T11:03:17+00:00

Why is my remove method removing every element from my Doubly Linked List? If

  • 0

Why is my remove method removing every element from my Doubly Linked List? If I take out that if/else statements then I can successfully remove middle elements, but elements at the head or tail of the list still remain. However, I added the if/else statements to take care of elements at the head and tail, unfortunately this method now removes every element in my list. What am I do wrong?

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

And my main class that I test my methods in is:

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

    }
}

The result that prints out after I run the program is:

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:03:18+00:00Added an answer on June 12, 2026 at 11:03 am

    By adding two if blocks to handle first and last, you have added 2 more executable blocks to run in addition to what was before. Now all three blocks are getting executed together. I believe you are trying to do something like this(execute only one block at a time):

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

    Please Note: When you are assigning head or tail in first if else conditions, at that time, remove_this is empty and hence making your head or tail as null and you print program is not printing anything.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a class that have a method of removing an object from other
In Iterator Sun added the remove method to remove the last accessed element of
Possible Duplicate: C++ Vector Pointers to Objects Does std::list::remove method call destructor of each
I've noticed that if I remove the action method declaration in the header file
I have this method which will remove all rows from a table but I
I'm having some trouble removing an element that gets added dynamically to the DOM.
Normally, in order to remove non-word characters from a String the replaceAll method can
In my application, I'm removing (or trying to remove) all records from two core
I would like to know an efficient method to remove duplicate items from a
I'd like to remove a class method that gets added to my class via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.