In: Computer Science
java circular linked list
/*
* Complete the playGame(int players, int passes) method
* Complete the addPlayers(int players) method
* Complete the passPotatoe(int passes) method
* No other methods/variables should be added/modified
*/
public class A3CircleLL {
/*
* Grading:
* Correctly uses helpers to play game - 1pt
* Prints correct winner when game is complete -
0.5pt
*/
public void playGame(int players, int passes) {
/*
* Use the helper methods addPlayers
and passPotatoe to play the game
* Continue passing the potato until
only 1 player remains
* Print the winning players
number
*
* For players = 5 and passes = 3,
the winner should be 1. Players should be removed in this
order:
* - 4, 3, 5, 2
*/
}
/*
* Grading:
* Correctly creates circular linked list of size
amount - 1pt
*/
private void addPlayers(int amount) {
/*
* Set up this method to create a
Node for each player
* The value of each Node, should be
the player number, starting at 1
* For example, if the amount is 5,
there should be Nodes 1-5
* Node 1 should always be set as
the start
* Make list circular by connecting
the last player Node to the first
*/
}
/*
* Grading:
* Correctly removes the player the number of passes
away from the start - 1pt
* Correctly changes the start to the player after the
one being removed - 0.5pt
*/
private void passPotato(int passes) {
/*
* Set up this method to play a
single round of the game
* Move through the list the number
of passes from the start
* Remove the player/Node at this
position
* Set the start equal to the
player/Node after this position
* Do not play a round if there is
one 1 player remaining
* Print the player number that was
removed and the player with the potato
*/
}
private Node start;
private int count;
public A3CircleLL() {
start = null;
count = 0;
}
public String printList() {
String output = "";
if(start != null) {
Node current =
start;
do {
output += current.value + ",";
current = current.next;
}while(current
!= start);
}
return output;
}
public String toString() {
return this.printList();
}
private class Node {
Integer value;
Node next;
public Node(Integer v) {
value = v;
next =
null;
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class A3Driver {
public static void main(String[] args) {
A3DoubleLL<Integer> list =
new A3DoubleLL<>();
for(int i = 1; i < 10; i++)
{
list.add(i);
}
System.out.println("Before
Swap");
System.out.println(list.printList());
System.out.println(list.printListRev());
list.swap(4);
System.out.println("After
Swap");
System.out.println(list.printList()
+ ":1,2,3,4,6,5,7,8,9,");
System.out.println(list.printListRev() +
":9,8,7,6,5,4,3,2,1,");
System.out.println();
System.out.println("Hot
Potatoe");
A3CircleLL hotPotato = new
A3CircleLL();
hotPotato.playGame(5, 3);
System.out.println("Correct:");
System.out.println("Removed Player
4\nRemoved Player 3\nRemoved Player 5\nRemoved Player 2\nWinning
player is 1");
System.out.println();
A3Queue<Integer> queue = new
A3Queue<>();
queue.enqueue(5);
queue.enqueue(20);
queue.enqueue(15);
System.out.println(queue.peek()+":5");
System.out.println(queue.dequeue()+":5");
queue.enqueue(25);
System.out.println(queue.dequeue()+":20");
System.out.println(queue.dequeue()+":15");
}
}
If you have any doubts, please give me comment...
/*
* Complete the playGame(int players, int passes) method
* Complete the addPlayers(int players) method
* Complete the passPotatoe(int passes) method
* No other methods/variables should be added/modified
*/
public class A3CircleLL {
/*
* Grading: Correctly uses helpers to play game - 1pt Prints correct winner when
* game is complete - 0.5pt
*/
public void playGame(int players, int passes) {
/*
* Use the helper methods addPlayers and passPotatoe to play the game Continue
* passing the potato until only 1 player remains Print the winning players
* number
*
* For players = 5 and passes = 3, the winner should be 1. Players should be
* removed in this order: - 4, 3, 5, 2
*/
addPlayers(players);
passPotato(passes);
System.out.println("Winning player is "+start.value);
}
/*
* Grading: Correctly creates circular linked list of size amount - 1pt
*/
private void addPlayers(int amount) {
/*
* Set up this method to create a Node for each player The value of each Node,
* should be the player number, starting at 1 For example, if the amount is 5,
* there should be Nodes 1-5 Node 1 should always be set as the start Make list
* circular by connecting the last player Node to the first
*/
for (int i = 0; i < amount; i++) {
Node newNode = new Node(count + 1);
if (start == null) {
start = newNode;
} else {
Node temp = start;
while (temp.next != start) {
// System.out.println(temp.value);
temp = temp.next;
}
// temp = temp.next;
temp.next = newNode;
}
newNode.next = start;
printList();
count++;
}
}
/*
* Grading: Correctly removes the player the number of passes away from the
* start - 1pt Correctly changes the start to the player after the one being
* removed - 0.5pt
*/
private void passPotato(int passes) {
/*
* Set up this method to play a single round of the game Move through the list
* the number of passes from the start Remove the player/Node at this position
* Set the start equal to the player/Node after this position Do not play a
* round if there is one 1 player remaining Print the player number that was
* removed and the player with the potato
*/
Node temp = start, prev = null;
while (temp.next != start || start.next != temp) {
for (int i = 0; i < passes; i++) {
prev = temp;
temp = temp.next;
}
System.out.println("Removed Player "+temp.value);
if(temp==start){
start = start.next;
}
else{
prev.next = temp.next;
}
temp = temp.next;
}
}
private Node start;
private int count;
public A3CircleLL() {
start = null;
count = 0;
}
public String printList() {
String output = "";
if (start != null) {
Node current = start;
do {
output += current.value + ",";
current = current.next;
} while (current != start);
}
return output;
}
public String toString() {
return this.printList();
}
private class Node {
Integer value;
Node next;
public Node(Integer v) {
value = v;
next = null;
}
}
}
public class A3Driver {
public static void main(String[] args) {
// A3DoubleLL<Integer> list = new A3DoubleLL<>();
// for (int i = 1; i < 10; i++) {
// list.add(i);
// }
// System.out.println("Before Swap");
// System.out.println(list.printList());
// System.out.println(list.printListRev());
// list.swap(4);
// System.out.println("After Swap");
// System.out.println(list.printList() + ":1,2,3,4,6,5,7,8,9,");
// System.out.println(list.printListRev() + ":9,8,7,5,6,4,3,2,1,");
// System.out.println();
System.out.println("Hot Potatoe");
A3CircleLL hotPotato = new A3CircleLL();
hotPotato.playGame(5, 3);
System.out.println("Correct:");
System.out
.println("Removed Player 4\nRemoved Player 3\nRemoved Player 5\nRemoved Player 2\nWinning player is 1");
System.out.println();
// A3Queue<Integer> queue = new A3Queue<>();
// queue.enqueue(5);
// queue.enqueue(20);
// queue.enqueue(15);
// System.out.println(queue.peek() + ":5");
// System.out.println(queue.dequeue() + ":5");
// queue.enqueue(25);
// System.out.println(queue.dequeue() + ":20");
// System.out.println(queue.dequeue() + ":15");
}
}