I took a different view with Delete States. I decided to implement it like the InsertAfter method by using a Boolean, but I am pretty sure I am doing it right, but it only displays one state after I implement it.

My Code for the Delete

/**
     *
     * @param key
     * @return
     */
    public boolean deleteKey(States key)   // delete item w/ given key
      {                              // (assumes non-empty list)
      States current = first;          // start at beginning
      while(current != null && current.getStateName().
              compareTo(key.getStateName())!=0)    
         {
         current = current.next;     // move to next link
         if(current == null) // didn't find it
         {
             System.out.println("Delete Key '" + 
                     key.getStateName() + "' Not Found");
             return false;             
         } //end if            
      if(current == first)       
         first = current.next; // first --> old next
      else     
         current.previous.next = current.next;// old previous --> old next 
      if(current == last)
         last = current.previous; // old previous <-- last
      else 
         current.next.previous = current.previous;  
         } //end while
      System.out.println("\n"); //separates display from other methods
      return true; // return value
      } //end deleteKey(States key)

My code in my controller class

public void removeDeleteStack()
    {   
        int successfulAttempts = 0;
        int unsuccessfulAttempts = 0;
        while (deleteStack.isEmpty())
        {
            System.out.println("\nStack for deleting items is empty.");
        }
        while(!deleteStack.isEmpty())
        {
        if(myList.deleteKey(deleteStack.pop()))
        {
            successfulAttempts++;
        }
        else 
        {
            unsuccessfulAttempts++;
        }
        }
        System.out.println("\nSuccessfully deleted: " + successfulAttempts);
        System.out.println("Key not Found, could not delete: " 
                + unsuccessfulAttempts);
    }
//------------------------------------------------------------------------------     
     public void finalDisplay()
     {
        System.out.println("\n Final List using Backward Pointers");
        myList.displayBackward();
     }

My output for these two methods

Delete Key 'Franklin' Not Found
Delete Key 'Tennessee' Not Found
Delete Key 'PA' Not Found
Delete Key 'Pennsylvania' Not Found

Successfully deleted: 0
Key not Found, could not delete: 4

Final List using Backward Pointers
        Alabama      Montgomery

My list output before these two methods

        Alabama      Montgomery
         Alaska            Juno
        Arizona         Phoenix
       Arkansas     Little_Rock
     California      Sacramento
       Colorado          Denver
    Connecticut        Hartford
       Delaware           Dover
        Florida     Tallahassee
        Georgia         Atlanta
         Hawaii        Honolulu
          Idaho           Boise
       Illinois     Springfield
        Indiana    Indianapolis
           Iowa      Des_Moines
         Kansas          Topeka
       Kentucky       Frankfort
      Louisiana     Baton_Rouge
          Maine         Augusta
       Maryland       Annapolis
  Massachusetts          Boston
       Michigan         Lansing
      Minnesota         St_Paul
    Mississippi         Jackson
       Missouri  Jefferson_City
        Montana          Helena
       Nebraska         Lincoln
         Nevada     Carson_City
  New_Hampshire         Concord
     New_Jersey         Trenton
     New_Mexico        Santa_Fe
       New_York          Albany
 North_Carolina         Raleigh
   North_Dakota         Bismark
           Ohio        Columbus
       Oklahoma   Oklahoma_City
         Oregon           Salem
   Pennsylvania      Harrisburg
     PuertoRico        San Juan
   Rhode_Island      Providence
 South_Carolina        Columbia
   South_Dakota          Pierre
      Tennessee       Nashville
          Texas          Austin
       Thailand         Bangkok
           Utah  Salt_Lake_City
        Vermont      Montpelier
       Virginia        Richmond
     Washington         Olympia
  West_Virginia      Charleston
      Wisconsin         Madison
        Wyoming        Cheyenne

Any idea why it's doing that?

It looks fine to me. My feeling is that your last variable is not stored correctly (the last variable is the same as the first variable node. However, you now need to debug what really happens.

You could take lines 13~18 outside of the while loop. The reason is that if current is null, it will break the loop anyway. If you leave the if-statement in there, it will be checked each iteration and most likely be false all the time.

About debugging... Could you insert these code lines in various places of your removeDeleteStack() method, and see what happens. These lines should confirm that your removeDeleteStack() is working correctly.

// add before line 9  (count from original line number)
myList.displayBackward();

// add before line 11  (count from original line number)
States k = deleteStack.pop();

// update line 11 to  (count from original line number)
if(myList.deleteKey(k))

// add before line 13  (count from original line number)
System.out.println("Successful: "+k.getStateName());

// add before line 17  (count from original line number)
System.out.println("Fail: "+k.getStateName());

After that, add these code lines in various places of your deleteKey() method to confirm that it works correctly. Also, please move the lines 13~18 out of the while loop and place them right below the while loop but before if(current==first) line.

// add before line 19  (count from original line number) right after while-loop
System.out.println("Current: "+(current==null ? "NULL" : current.getStateName));

// should have moved the if(current==null) out here

// add before line 19  (count from original line number) after
// the moved `if(current==null)` block
System.out.println("Previous1: "+(current.previous==null ? "NULL" : current.previous.getStateName()));
System.out.println("Next1: "+(current.next==null ? "NULL" : current.next.getStateName()));

// add before line 23  (count from original line number)
System.out.println("Previous2: "+(current.previous==null ? "NULL" : current.previous.getStateName()));
System.out.println("Next2: "+(current.next==null ? "NULL" : current.next.getStateName()));

// add before line 28  (count from original line number)
System.out.println("Previous3: "+(current.previous==null ? "NULL" : current.previous.getStateName()));
System.out.println("Next3: "+(current.next==null ? "NULL" : current.next.getStateName()));

Please let me know what you see for the result.

Edited 4 Years Ago by Taywin

Before I used the debugging methods I figured I would change the while loop like you said. The While loop should have ended at line 14 not at the end. I completely missed that for some reason. Below is now my output.

Delete Key 'Franklin' Not Found
Delete Key 'PA' Not Found

Successfully deleted: 2
Key not Found, could not delete: 2

 Final List using Backward Pointers
        Wyoming        Cheyenne
      Wisconsin         Madison
  West_Virginia      Charleston
     Washington         Olympia
       Virginia        Richmond
        Vermont      Montpelier
           Utah  Salt_Lake_City
       Thailand         Bangkok
          Texas          Austin
   South_Dakota          Pierre
 South_Carolina        Columbia
   Rhode_Island      Providence
     PuertoRico        San Juan
         Oregon           Salem
       Oklahoma   Oklahoma_City
           Ohio        Columbus
   North_Dakota         Bismark
 North_Carolina         Raleigh
       New_York          Albany
     New_Mexico        Santa_Fe
     New_Jersey         Trenton
  New_Hampshire         Concord
         Nevada     Carson_City
       Nebraska         Lincoln
        Montana          Helena
       Missouri  Jefferson_City
    Mississippi         Jackson
      Minnesota         St_Paul
       Michigan         Lansing
  Massachusetts          Boston
       Maryland       Annapolis
          Maine         Augusta
      Louisiana     Baton_Rouge
       Kentucky       Frankfort
         Kansas          Topeka
           Iowa      Des_Moines
        Indiana    Indianapolis
       Illinois     Springfield
          Idaho           Boise
         Hawaii        Honolulu
        Georgia         Atlanta
        Florida     Tallahassee
       Delaware           Dover
    Connecticut        Hartford
       Colorado          Denver
     California      Sacramento
       Arkansas     Little_Rock
        Arizona         Phoenix
         Alaska            Juno
        Alabama      Montgomery

It was supposed to remove Pennsylvania and Tennessee and it did. It works perfectly now.

This question has already been answered. Start a new discussion instead.