Question

Java please. Need to use a linked list only. Many people have used pulling petals off...

Java please. Need to use a linked list only.

Many people have used pulling petals off roses and tossing them in the air to determine if someone loves them or not. Your sister wants to use roses to determine which one of several suitors she will let take her to the prom. A linked list can be used to represent the prom date suitors. Write a program to simulate her decision and determine her prom date. The simulation will work as follows:

• She has many roses, and she knows how many petals are on each rose.

• The suitors are numbered from 1 to n, where n is the number of suitors.

• She will select one rose and give one petal to suitor #1 and continuing successively through suitors until she runs out of petals. When she reaches the end of the list, she begins over with the beginning of the list.

• The person who gets the last petal is eliminated from consideration.

• She gets another rose and continues the process beginning with the suitor after the one that was eliminated. •

She continues this process until there is only one suitor left – he is her date for the prom.

Input will come from a text file. The first line will contain a single integer n that indicates the number of test cases to be played. For each test case, there are 2 lines. The first line is the list of the first names of the suitors, separated by a comma and a space. The second line has the number of petals on each of the roses separated by a space. The first rose will be used to find the first suitor to be eliminated, the second rose will be used to find the second person to be eliminated, etc.

Note: There is one less rose than suitors. Output to the screen, each test case labeled, then display the order of the persons eliminated as LOSER name, one per line, with the final line naming the winner as WINNER name.

Place white space between test cases. Let the user input the file name from the keyboard. The program must use a linked list.

Sample File:

1

ALAN, BRYAN, CHAD, DUKE, ERIC, FRED

100 234 564 234 1231

Sample Run:

Enter file name: dates.txt

Case 1:

LOSER DUKE

LOSER BRYAN

LOSER ALAN

LOSER FRED

LOSER CHAD

WINNER ERIC

Homework Answers

Answer #1

I have implemented it using Circular Linked list:

class CLLOperations{
          Node head=null,tail=null;
          //adding new node to CLL
      public void addNewNode(String value){
          Node newNode = new Node(value);
          if(this.head==null){
                  this.head=newNode;
                  this.tail=newNode;
                  newNode.nextNode=head;
          }else{
                  this.tail.nextNode=newNode;
                  this.tail= newNode;
                  this.tail.nextNode=head;                
          }
          System.out.println("Node "+value+" is added succesfully");
      }     
      //Displaying all nodes until count is 20 as per given question
      public void display(){
          Node current = this.head;
          if(this.head==null){
                  System.out.println("list is empty");
          }else{
                  for(int i=0;i<20;i++){
                          System.out.println(current.value);
                          current=current.nextNode;
          }
        }         
      }
      public void remove(String n){
          Node curr = head,prev=null;
          if(head==null){
                  return;
          }else{
                  while(!curr.value.equals(n)){
                          if(curr.nextNode==head){
                                  break;
                          }
                                prev=curr;
                                curr=curr.nextNode;
                          }
                     if (curr.nextNode == head) { 
                    head = null; 
                    
                } 
                     if (curr == head) { 
                            prev = head; 
                            while (prev.nextNode != head) 
                                prev = prev.nextNode; 
                            head = curr.nextNode; 
                            prev.nextNode = head; 
                       } 
                     else if (curr.nextNode == head) { 
                            prev.nextNode = head; 
                        } else { 
                            prev.nextNode = curr.nextNode; 
                        }                 
                  }               
          }
     public void rosePetalGame(String[] str, String[] petals){
         Node current = this.head;
         for(int i=0;i<petals.length;i++){
            for(int j=0;j<(Integer.parseInt(petals[i])-1);j++){
                current=current.nextNode;
            }
            Node lose=current;
            current=current.nextNode;
            remove(lose.value);             
            System.out.println("Loser "+lose.value);
         }  
         System.out.println("Winner"+current.value);
     }
}   

class Node {
      String value;
      Node nextNode;
      Node (String v){
          this.value=v;
      } 
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class CircularLinkedList{
        public static void main(String[] args) throws FileNotFoundException{
                String s;
                CLLOperations cl = new CLLOperations();
                File file = new File("./data.txt"); 
        Scanner sc = new Scanner(file); 
        String names[] = sc.nextLine().split(", ");
        String petals[] = sc.nextLine().split(" ");
        for(int i=0;i<names.length;i++)
                cl.addNewNode(names[i]);
        System.out.println("------------------------------------");
        cl.rosePetalGame(names,petals);
    }
}

The output after execting given program is:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT