Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Ken s office hours are getting really full, and a long line is forming outside his office each time. The students decide that they are

Kens office hours are getting really full, and a long line is forming outside his office each time. The students decide that they are too smart to simply line up, and decide on implementing a system. The system is a queue, meaning the first student in the line is the frist person Ken will see. However, to make the system better, the students decide on adding two rules:
1. When a student joins the back of the line, if they are younger than the person in front of them they can move up one spot.
2. When a student joins the line and their grade in the class is lower than the person in front of them, they can also move up one spot.
Input Format
If a student is joining the line, the input format is the students first name then a space then their age, then a space, then their grade. When Ken helps the next student in line, removing them from the queue, the input is next. When the input is done, the input is end.
Constraints
The student name is always less than 100 characters.
The student age is always less than 250 years.
The student grade is always in between 0 and 100 inclusive.
Output Format
The output will be the order of the students currently in line. If the queue is empty, output the word empty.
Sample Input 0
Rob 3398
Sarah 29100
Dave 4327
Kodi 2592
next
Shane 3297
Jodie 2995
end
Sample Output 0
Dave 4327
Kodi 2592
Shane 3297
Jodie 2995
Rob 3398
Explanation 0
Rob joins the line,
1. Rob
Sarah, being younger than Rob, moves up into first in line.
1. Sarah, 2. Rob
When Dave joins the line, his grade is worse than the person in front of him, so Dave moves in front of Rob.
1. Sarah, 2. Dave, 3. Rob
Kodi joins the line, but is younger & has a lower grade than Rob.
1. Sarah, 2. Dave, 3. Kodi, 4. Rob
Ken calls next, meaning the front of the line is removed.
1. Dave, 2. Kodi, 3. Rob
1. Dave, 2. Kodi, 3. Shane, 4. Rob
1. Dave, 2. Kodi, 3. Shane, 4. Jodie, 5. Robimport java.io.*;
import java.util.*;
class Student{
String name;
int age;
int grade;
public String toString(){
return name +""+ age +""+ grade;
}
}
class Node{
Student student;
Node next;
}
public class Solution {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
Node head = null;
while(true){
String name = scan.next();
if(name.equals("next")){
head = head.next;
}
else if(name.equals("end")){
break;
}
else{
Student student = new Student();
student.name = name;
student.age = scan.nextInt();
student.grade = scan.nextInt();
Node tempNode = new Node();
tempNode.student = student;
tempNode.next = null;
if(head == null){
head = tempNode;
}
else{
Node previous = head;
while(previous.next != null){
previous = previous.next;
}
previous.next = tempNode;
if(previous.student.age > tempNode.student.age || previous.student.grade > tempNode.student.grade){
Student tempStudent = previous.student;
previous.student = tempNode.student;
tempNode.student = tempStudent;
}
}
}
}
Node iter = head;
while(iter != null){
System.out.println(iter.student.toString());
iter = iter.next;
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL/PHP Database Applications

Authors: Jay Greenspan, Brad Bulger

1st Edition

ISBN: 978-0764535376

More Books

Students also viewed these Databases questions