Below is how the the hot potato should be passing and deletion should work:
N people, numbered 1 to N, are sitting in a circle. Person 1 picks up a hot potato and passes it to person 2, who passes it to person 3, and so on. After M passes, the person holding the hot potato is eliminated, the circle closes ranks and the play continues by having the person sitting after the eliminated person pick up the potato and resuming the pass (picking up the potato does not count as one of the M passes). The last person remaining wins the game.
I have got my code this far but now im stuck with the deletion part. If you look at the bottom of the code you will find where I am trying to perform the passes and deletions. Can you guys and gals look and give me something please!?
Thanks everyone!
Andrew
import java.util.Scanner;
class Link
{
public int iData;
public Link next;
// -------------------------------------------------------------
public Link(int id, Link dd)
{
iData = id;
next = dd;
}
// -------------------------------------------------------------
public void displayLink()
{
System.out.print(iData + " ");
}
} // end class Link
////////////////////////////////////////////////////////////////
class LinkList
{
public Link first = null;
public Link last = null;
public LinkList()
{
first = null;
}
public void insert(int id)
{
Link newLink = new Link( id, null );
newLink.next = first;
first = newLink;
}
public Link find(int key)
{
Link current = first;
while(current.iData != key)
{
if(current.next == null)
return null;
else
current = current.next;
}
return current;
}
public Link delete(int key)
{
Link current = first;
Link previous = first;
while(current.iData != key)
{
if(current.next == null)
return null;
else
{
previous = current;
current = current.next;
}
}
if(current == first)
first = first.next;
else
previous.next = current.next;
return current;
}
public void displayList()
{
System.out.print("Players left: ");
Link current = first;
while(current != null)
{
current.displayLink();
current = current.next;
}
System.out.println("");
}
} // end class LinkList
////////////////////////////////////////////////////////////////
public class LinkedList
{
public static void main(String[] args)
{
new LinkedList( );
}
public LinkedList( )
{
// instantiate Scanner object
Scanner input = new Scanner( System.in );
LinkList theList = new LinkList();
int min = 1;
int top = 0;
int current;
int players;
int passes;
// input number of players and passes
System.out.print( "Enter number of players: " );
players = input.nextInt( );
top = players;
System.out.print( "Enter number of passes: " );
passes = input.nextInt( );
while ( min <= top )
{
theList.insert(top);
top--;
} // end while
System.out.println( "Number of players is " + players );
System.out.println( "Number of passes is " + passes );
current = passes+1;
while ( players >= min)
{
theList.displayList();
Link d = theList.delete(current);
if( d != null )
System.out.println("Player " + d.iData + " is out!");
current = d.iData + (passes+1);
if( current > players )
current = d.iData + (passes+1) - players;
players--;
}
}
} // end class LinkedList

New Topic/Question
Reply




MultiQuote







|