• Home
  • Blog
  • Linked Lists Method Question (need help) [code provided]

Linked Lists Method Question (need help) [code provided]

0 comments

Hey guys, below is a piece of code. Underneath that is what I am trying
to accomplish with my code, but am confused on what to do. Can someone
please help?

public class PlayerLinkedList extends ShellLinkedList
{

  public PlayerLinkedList(  )
  {
  super( );
  }

  public void insert( Player p )
  {
  // insert as head
  PlayerNode pn = new PlayerNode( new Player(  p ) );
  pn.setNext( head );
  head = pn;
  numberOfItems++;
  }

  public Player delete( int searchID )
  throws DataStructureException
  {
  PlayerNode current = head;
  PlayerNode previous = null;
  while ( current != null
  && current.getPlayer( ).getID( ) != searchID )
  {
  previous = current;
  current = current.getNext( );
  }

  if ( current == null ) // not found
  throw new DataStructureException( searchID
  + " not found: cannot be deleted" );
  else
  {
  if ( current == head )
  head = head.getNext( );  // delete head
  else
  previous.setNext( current.getNext( ) );

  numberOfItems--;
  return current.getPlayer( );
  }
  }

 
  public Player peek( int searchID )
  throws DataStructureException
  {
  PlayerNode current = head;
  while ( current != null
  && current.getPlayer( ).getID( ) != searchID )
  {
  current = current.getNext( );
  }

  if ( current == null ) // not found
  throw new DataStructureException( searchID
  + " not found: cannot be deleted" );
  else
  {
  return current.getPlayer( );
  }
  }
}


I want to include one more method that inserts a new player in the third
position of the list. Head would be the first position. If the list is
empty, the method will insert the new player as the head of the list.

Can anyone please help me with this? I would really appreciate it.

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}