1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Vilka [71]
2 years ago
9

In an ancient land, the beautiful princess Eve (or handsome prince Val) had many suitors. Being royalty, it was decided that a s

pecial process must be used to determine which suitor would win the hand of the prince/princess. First, all of the suitors would be lined up one after the other and assigned numbers. The first suitor would be number 1, the second number 2, and so on up to the last suitor, number n. Starting at 4 the suitor in the first position, she/he would then count three suitors down the line (because of the three letters in his/her name) and that suitor would be eliminated and removed from the line. The prince/princess would then continue, counting three more suitors, and eliminate every third suitor. When the end of the line is reached, counting would continue from the beginning. For example, if there were 6 suitors, the elimination process would proceed as follows:_____.
12456 Suitor 3 eliminated; continue counting from 4.
1245 Suitor 6 eliminated; continue counting from 1.
125 Suitor 4 eliminated; continue counting from 5.
15 Suitor 2 eliminated; continue counting from 5.
1 Suitor 5 eliminated; 1 is the lucky winner.
Write a program that creates a circular linked list of nodes to determine which position you should stand in to marry the princess if there are n suitors. Your program should simulate the elimination process by deleting the node that corresponds to the suitor that is eliminated for each step in the process.
Computers and Technology
1 answer:
maks197457 [2]2 years ago
7 0

Explanation:

public class CircularLinkedListTest  

{

private Node head;

private int size;

private class Node

{

 private int num;

 private Node next;

 public Node(int n)

 {

  num = n;

  next = null;

 }

 public int getNum()

 {

  return num;

 }

 public void setNext(Node n)

 {

  this.next = n;

 }

 public Node getNext()

 {

  return next;

 }

}

public CircularLinkedListTest ()

{

 head = null;

 int numNodes = 0;

}

public void add(int num)

{

 int numNodes = 0;

 if(head == null)

 {

  head = new Node(num);

  head.setNext(head);

  numNodes++;

 }

 else

 {

  Node temp = head;

  while(temp.getNext() != head)

   temp = temp.getNext();

   

  temp.setNext(new Node(num));

  temp.getNext().setNext(head);

  numNodes++;

   

 }

}

public int size()

{

 int numNodes = 0;

 return numNodes;

}

public int get(int index)

{

 Node t = head;

 for(int i = 0; i < index; i++)

  t= t.getNext();

 

 return t.getNum();

}

public int remove(int index)

{

 if(index < 0 || index >= size)

 {

  System.out.println("Error, index out of Qbounds.");

  System.exit(0);

 }

 Node temp = head;

 if(index == 0)

 {

  while(temp.getNext() != head)

   temp = temp.getNext();

  int value = head.getNum();

  if(size > 1)

  {

   head = head.getNext();

   temp.setNext(head);

  }

  else

   head = null;

   size--;

   

  return value;

 }

 else

 {

  for(int i = 0; i < index - 1; i++)

   temp = temp.getNext();

   

  int answer = temp.getNext().getNum();

  temp.setNext(temp.getNext().getNext());

  size--;

  return answer;

 }

}

public static void main(String args[])

{

 CircularLinkedListTest  suitors = new CircularLinkedListTest ();

 for(int i = 1; i <= 6; i++)

  suitors.add(i);  

 int currentIndex = 0;

 while(suitors.size() != 1)

 {

  for(int i = 1; i <= 2; i++)

  {

   currentIndex++;

   if(currentIndex == suitors.size())

    currentIndex = 0;

  }

   

  suitors.remove(currentIndex);

  if(currentIndex == suitors.size())

   currentIndex = 0;

   

  for(int i = 0; i < suitors.size(); i++)

   System.out.print(suitors.get(i) + " ");

  System.out.println();

 }

}

}

You might be interested in
What's is flow chart?
TEA [102]

Answer: A flowchart is a graphical representation of decisions and their results mapped out in individual shapes.

Explanation:

These shapes were first developed by Herman Goldstine and John von Neumann in the 1940s.

Flowcharts can provide a step-by-step diagram for mapping out complex situations, such as programming code or troubleshooting problems with a computer.

4 0
3 years ago
The memory address that is saved by the system when a method is called and is the location to which the system should return aft
MA_775_DIABLO [31]

Answer:

Return point

Explanation:

4 0
2 years ago
A digital _ Is a good place for students to read an out of print book
bonufazy [111]

Answer:

Library

Explanation:

7 0
3 years ago
Read 2 more answers
R-410A Is also known as A. Suva 410a B. AZ-20 C. Puron D. All Of The Above
Rama09 [41]
I believe it's D. Hope this helps.
8 0
3 years ago
Please don’t comment if you don’t know the answer.
abruzzese [7]

Answer:If your notebook computer battery does not power the notebook or will not hold a charge, troubleshoot the battery to see if it needs to be replaced. If the battery indicator LED, located near the power icon, does not glow or always blinks, there is a battery problem. Your computer might operate correctly when it is connected to the power adapter, but not when using battery power.

Explanation:

7 0
3 years ago
Other questions:
  • Musccanic Inc., a company that manufactures microprocessors, updates the technology used in its microprocessors once every four
    15·1 answer
  • Greg works for online games development company. He occasionally visits online literature sites and downloads e-books of his cho
    12·2 answers
  • Taken together, the physical and data link layers are called the ____________________. Internet layer Hardware layer Internetwor
    15·1 answer
  • Why are duplicate tuples not allowed in a relation?
    6·1 answer
  • A researcher plans to identify each participant in a certain medical experiment with a code consisting of either a single letter
    10·1 answer
  • What is project scope? a. Quantifiable criteria that must be met for the project to be considered a success b. Products, service
    5·1 answer
  • B. What significant values have you learned while learning the tools and utensils?
    8·1 answer
  • Currently, this program will add 6 and 3 together, output the math problem and output the answer. Edit this code so that a rando
    9·1 answer
  • Select the correct answer.
    5·1 answer
  • What is a best practice when approaching an automation effort?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!