This post was posted by "Brian" and deleted from StackOverflow by the same. I'm re-posting it so that his instructor may be able to find it:
http://stackoverflow.com/questions/1...bly-linkedlist


I need to remove an element from my doubly linkedlist without using Java Generics, but instead using my own, personally defined method. Below is my code and I have two different remove methods I'm working with:

public E remove(int n)

public void remove(int n)

public void remove2(int n)

If you can help me out with any of the above methods I would love you forever. I've spent almost a week trying to figure them out, but still cant.

I think I'm close to the correct way of defining the method, but my pointers don't seem to be acting correctly. If you could help me out I would really appreciate it. Also, please don't send me links on the logic behind removing elements, I learn better from looking at actual code that pertains to my specific problem. Thanks for any help you can provide.

Java Code:
    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;
    }

    /*
     * ~~~~~~~~BEGINNING OF REMOVE METHODS~~~~~~~~
     */

       /*
        * Remove the nth element in the list.  The first element is element 1.
        * Return the removed element to the caller.
        */
       public void remove2(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;
    	      }
          }
       }
   
       public void remove(int n)
       {
	       LinkEntry<E> current = new LinkEntry<E>();
	       for (int i = 0; i < size(); i++)
	       {
		       if (i == n)
		       {
			       current.previous.next = current.next;
			       current.next = current.next.next;
			       current.previous = current.next;
		       }
	       }
       }
   
       public void remove(E e){
	 	  
	 	      LinkEntry<E> tag = new LinkEntry<E>();
	 	      LinkEntry<E> temp = new LinkEntry<E>();
	 	      temp = head;
	 	  
	 	      //checking for element in head spot
	 	      if (head.element.equals(e) && head != null){
	 		      head = head.next;
	 		      size--;
	 	      }
	 	      //checking for element in tail spot
	 	      else if (tail.element.equals(e)){
	 		      tail = tail.previous;
	 		      tail.next = null;
	 		      size--;
	 	      }
	 	      while (temp != null && !temp.element.equals(e)){
	 		      temp.previous = tag;
	 		      tag = temp;		//used as a reference to the element before e
	 		      temp = temp.next;	//temp is being removed
	 	      }
	    }

    /*
     * ~~~~~~~~END OF REMOVE METHODS~~~~~~~~
     */

       /*
        * 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> */

_________________________________________________

Answer posted:

Java Code:
    public void remove2(int n)
    {
      LinkEntry<E> current = new LinkEntry<E>();
      int i = 0;

      while (n == i++)
      {
          current.previous.next = current.next;
          if (current.next == null)
          {
    // If current.next is NULL, how do you expect the machine to set its "previous" field?
    // In other words: is there a NULL.previous ?
              current.next.previous = current.previous;
          }
      }
    }
I see that your problem is **exactly** the "logic behind `remove`". Do the math or you **will** fail not only in midterms but also in your future carreer.

I gave you a hint in above code, because I _like_ to help. But I expect some effort. This community does not like "feeding code". We are here to learn and help others learn. So this is the colsest to an answer I can give you.

Good luck.

____________________________________

Some comments:
"Also, please don't send me links on the logic behind removing elements, I learn better from looking at actual code that pertains to my specific problem." -- !!!! Note that you'll do better learning from tutorials and theory if you did more reading of tutorials and theory. This is a "muscle" that needs exercising, not avoiding.

This is clearly a homework problem, and the point of this homework problem is to understand exactly how the pointers work.

Until you understand and can explain what you are doing without a computer, you can't get a computer to do it for you. You need the logic explanations, a lot more than you think you do. With understanding of a problem, you can solve it in any language, without understanding you can only cut and paste code you hope solves the problem. What kind of education are you going to pay for?