In: Computer Science
Description: |
The students in a class would really like it if Professor NotHearingIt would move an homework assignment back a week. None of the students wish to confront the professor and so decided to get in a circle and remove every Nth student until only one is left. This student is the one who must go ask for the extension. One student, Joseph, does not actually wish to move the assignment back. At the same time he does not want the other students to know that he is the only one who feels this way. He does not know what number N will be decided for deciding speaker until the process begins. He wants to know where to stand in the circle so that he will be chosen to go speak to the professor so the assignment will not change. You are to build the simulation to help Joseph decided where to stand for any given N value. |
Program: |
You are to create a file called CircleQueue.java. You will create two class CircleQueue and Node. The queue will be a circular queue with the nodes storing the students’ names and starting position. This queue must be reference based since you will not know how many students to have at the beginning. Your Node should be generic but have two generic values. The students name will be a string while starting position will be an integer, but the node should be generic and accept any two values. You are given a file called names.txt. This file contains the name of each student on a line. You must read in each student and add him or her to the queue. The node created will contain the student’s name as well as some integer that keeps track of the order added to the queue as the starting position. NOTE: this starting position will not change will NOT change after set. The first position will be 0 and the last will be size – 1. Once the queue is filled you will go through and start removing students. First ask the user to enter the N value to be the counter for removing students (for example an N of two will remove person 2,4,6,etc…) Start with the first person and dequeue him or her. If the person does not meet the order corresponding to the number, add the student back to the queue. If the student does correspond to the N value, excuse the student and do not add him or her back. Do this until you are left with one student, and display the name and starting position of the student who must go talk to the professor. |
Input: |
There is an input file called names.txt were each line contains a name. These are the names in the file: Sammy |
Output: |
Enter the number used to remove people from list: 2 Sammy is excused. Susie is excused. Angela S. is excused. George is excused. Thomas the Great is excused. Jack Spratt is excused. Steven is excused. Chuckles the Clown is excused. Joseph is excused. Jim Bob is excused. What's His Name is excused. Eileen is excused. Elizabeth Anne is excused. Kenny is excused. Daniel is excused. Mr. Know-it-All is excused. Ruth is excused. Robert : 4 must go talk to the instructor. |
public class CircleQueue {
// Node class to store data
static class Node
{
public String name;
public int position ;
public Node next;
public Node( String name,int data
)
{
this.name=name;
this.position =
data;
}
}
/* Function to find the only person left
after one in every m-th node is removed
in a circle of n nodes */
static void lastPosition(int m, int
n,List<String> arr)
{
// Create a circular linked list
of
// size n.
Node head = new
Node(arr.get[0],0);
Node prev = head;
for(int i = 1; i < n; i++)
{
prev.next = new
Node(arr.get(i),i);
prev =
prev.next;
}
// Connect last node to first
prev.next = head;
/* while only one node is left in
the
linked list*/
Node ptr1 = head, ptr2 =
head;
while(ptr1.next != ptr1)
{
// Find m-th
node
int count =
1;
while(count !=
m)
{
ptr2 = ptr1;
ptr1 = ptr1.next;
count++;
}
/* Remove the
m-th node */
ptr2.next =
ptr1.next;
ptr1 =
ptr2.next;
}
System.out.println
(ptr1.name+":"+ptr1.position+" must go talk to the instructor");
//last position
}
/* Driver program to test above functions */
public static void main(String args[])
{
BufferedReader abc = new BufferedReader(new
FileReader("names.txt"));
List<String> lines = new ArrayList<String>();
while((String line = abc.readLine()) != null) {
lines.add(line);
}
abc.close();
Scanner sc = new Scanner(System.in);
int n = lines.size(), m
=sc.nextInt();
if(n!=0)
lastPosition(m, n, lines); //pass
list,listsize and m(positions to remove)
}
}