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

User Generated

xevfuan9312

Programming

Description

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.

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

You should ask your programming questions at likeplum. I always get good answers there to my programming issues and questions. Especially about php, js or html questions…They have programmers online to help you asap. It's free.

https://www.likeplum.com/user/learn/programming/aid/2

Related Tags