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 Data Structure Doubly Linked List /* * Complete the swap(int index) method * No other...
Java Data Structure Doubly Linked List /* * Complete the swap(int index) method * No other methods/variables should be added/modified */ public class A3DoubleLL<E> {    /*    * Grading:    * Swapped nodes without modifying values - 2pt    * Works for all special cases - 1pt    */    public void swap(int index) {        //swap the nodes at index and index+1        //change the next/prev connections, do not modify the values        //do not...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method * Complete the peek() method * No other methods/variables should be added/modified */ public class A3Queue {    /*    * Grading:    * Correctly adds an item to the queue - 1pt    */    public void enqueue(E val) {        /*        * Add a node to the list        */    }    /*    * Grading:   ...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method * Complete the peek() method * No other methods/variables should be added/modified */ public class A3Queue {    /*    * Grading:    * Correctly adds an item to the queue - 1pt    */    public void enqueue(E val) {        /*        * Add a node to the list        */    }    /*    * Grading:   ...
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...
In JAVA: Create a circular doubly linked list. It need not be generic. Implement addToStart and...
In JAVA: Create a circular doubly linked list. It need not be generic. Implement addToStart and addToEnd methods, as well as printList method. Implement delete(Node n) method that deletes a node n, if n is in the linked list. Make no assumptions about n. Test your linked list.
Java coding: 2. Write a method which takes a list list of int , and reverse...
Java coding: 2. Write a method which takes a list list of int , and reverse it. // recursion 3.Write a method which takes a list list of strings , and reverse it. // in different way than the previous 3. Write a two methods which take a list and find the largest integer number in it.
method to remove all elements frrom my linked list (java)
method to remove all elements frrom my linked list (java)
One way to implement a queue is to use a circular linked list. In a circular...
One way to implement a queue is to use a circular linked list. In a circular linked list, the last node’s next pointer points at the first node. Assume the list does not contain a header and that we can maintain, at most, one iterator corresponding to a node in the list. For which of the following representations can all basic queue operations be performed in constant worst time? Justify your answers. Maintain an iterator that corresponds to the first...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT