Question

In: Computer Science

java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the...

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");

   }
}

Solutions

Expert Solution

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");

    }

}


Related Solutions

Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
java Modify doubly Linked List code to include following index (rank) based access operations int RetrieveAt(int...
java Modify doubly Linked List code to include following index (rank) based access operations int RetrieveAt(int index) void DeleteAt(int index) void Swap(int index, int index) index starts at 0 (just like array's subscript) If the index is out of bound, RetrieveAt returns 0, DeleteAt does nothing, do nothing in Swap. Write your own testing program to test the modified class -----------------------------------------DLinkedlist.java---------------------------- public class DLinkedList { private class Node { String data; Node next; Node prev; public Node(String s) { data...
Just that method --> Java code Write a method “int sumPos(List aList)” to calculate the sum...
Just that method --> Java code Write a method “int sumPos(List aList)” to calculate the sum of positive integers in an ADT List aList of integers using ADT List operations. ADT List operations: isEmpty(), size(), add(index, item), remove(index), get(index), and removeAll(). You should not assume how an ADT List is implemented. Using array indexing, head/tail references, or any operation not defined in ADT List is not allowed.
Write a java method to swap between two values in a singly linked list
Write a java method to swap between two values in a singly linked list
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
Java program to implement circular linked list. NO COPY PASTE ANSWERS plz follow the given template......
Java program to implement circular linked list. NO COPY PASTE ANSWERS plz follow the given template... public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last()...
Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next b. Singly Linked List has two private instance variables first and last as that point to the first and the last nodes in the list, respectively. Write a fragment of code that removes the last node in a linked list whose first node is first....
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method receives the following arrangement: [1,2,3,4,5] The invert method returns the array [5,4,3,2,1] Example 2: the print method receives the following array: [5,4,3,2,1] The print method prints: 5,4,3,2,1 (data separated by commas). TIP: for the print method use System.out.print () without the "ln".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT