I need to get specific objects deleted from a LinkList, but the deleted items come from a stack of sorted State objects, but the Statename is the only value. I am trying to compare the statenames when using deleteKey, but for some reason it is not working at all. I tried using ==, CompareTo, .equals and nothing even showed up or tried to compare.

public States deleteKey(States key)   // delete item w/ given key
      {                              // (assumes non-empty list)
      States current = first;          // start at beginning
      while(current.next.getStateName.equals(key.getStateName))    
         {
         current = current.next;     // move to next link
         if(current == null) 
         {
             System.out.println("Delete Key '" + key.getStateName() + "' Not Found");
             return null;

         }             // didn't find it
         }
      if(current.next.getStateName.equals(first.getStateName))             // found it; first item?
         first = current.next;       // first --> old next
      else                           // not first
                                     // old previous --> old next
         current.previous.next = current.next;       

      if(current.next.getStateName.equals(last.getStateName))              // last item?
         last = current.previous;    // old previous <-- last
      else                           // not last
                                     // old previous <-- old next
         current.next.previous = current.previous;   
      return current;                // return value
      }

My method to implement this in my Controller class is

public void removeDeleteStack()
    {   
        for(int index=0; //index is a local variable for state objects
                index < deleteStackItems;
                index++) 
        {
            myList.deleteKey(deleteStack.pop());
        }
        myList.displayForward();

My method for deleteStack.pop is(deleteStack is the stack created with items to delete)

public States pop()            // take item from top of stack
      {
          return theList.deleteFirst();
      }

My method for deleteFirst is(theList is a LinkList implemented as a stackRequirement)

public States deleteFirst()         // delete first link
      {                              // (assumes non-empty list)
      States temp = first;  // will return temp to calling environment.  
      if(first.next == null)         // if only one item
         last = null;                // null <-- last        
      else
         first.next.previous = null; // null <-- old next
      first = first.next;            // first --> old next
      return temp;
      }

Any help is much appreciated. Also I have to insert State objects into the link list, but the linklist is sorted(let me know if I should place this in a separate thread).

public void insertAddStack()
    {   
        int probe=0, index=0;
        States key=null;

        for (;
                index < addStackItems;
                index++) //loops through keys
        {
            for (;
                    probe < numStates;
                    probe++)
            {
            if (myStates[index].getStateName().equals
               (addStack.pop().getStateName())) //compares stateNames
                    break; //ends inner for loop
            }
        }//end inner for loop
            if (probe == numStates) 
                // returns can not find key when probes equal number of states
            {
                myList.insertAfter(key, addStack.pop());<---the Key here should be set to a 
                                                            State object to add the popped object after
                                                            I cannot figure out how I would get that. I'm 
                                                            not asking for code, but just to be pointed in 
                                                            the right direction here. I know I need two  
                                                            pointers it needs to do a search(binary maybe?) 
                                                            and then possibly use the comparable startswith 
                                                            method, but I know I can't use startswith "A" 
                                                            or something it has to compare and place this 
                                                            object in the right order(a few states start 
                                                            with "A")
            }//end if 
            else //returns found key with linear probe count 
            {
                System.out.println("Dupe Add Attempted"
                        + "\nFound " + addStack.pop().getStateName());
            }//end else

Recommended Answers

All 2 Replies

Just had a very quick look - but shouldn't the while on the first line 4 be
while( ! current.next.getStateName.equals(key.getStateName))
(as you coded it, if the first entry doesn't match you will immediately exit the loop)

I edited it a bit, but I still can't get rid of this nullpointer exception.

public States deleteKey(States key)   // delete item w/ given key
      {                              // (assumes non-empty list)
      States current = first;          // start at beginning
      while(!key.getStateName().equals(current.next.getStateName()))    
         {
         current = current.next;     // move to next link
         if(current == null) 
         {
             System.out.println("Delete Key '" + 
                     key.getStateName() + "' Not Found");
             return null;

         }             // didn't find it

      if(current.next == first) 

         first = current.next; // first --> old next
      else 

         current.previous.next = current.next;// old previous --> old next 
      if(current.next == last)
         last = current.previous; // old previous <-- last
      else 
         current.next.previous = current.previous;<--------ERROR:NULLPOINTEREXCEPTION  
         }
      return current; // return value
      }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.