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?