Answered step by step
Verified Expert Solution
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:
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.
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 characters.
The student age is always less than years.
The student grade is always in between and 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
Rob
Sarah
Dave
Kodi
next
Shane
Jodie
end
Sample Output
Dave
Kodi
Shane
Jodie
Rob
Explanation
Rob joins the line,
Rob
Sarah, being younger than Rob, moves up into first in line.
Sarah, Rob
When Dave joins the line, his grade is worse than the person in front of him, so Dave moves in front of Rob.
Sarah, Dave, Rob
Kodi joins the line, but is younger & has a lower grade than Rob.
Sarah, Dave, Kodi, Rob
Ken calls next, meaning the front of the line is removed.
Dave, Kodi, Rob
Dave, Kodi, Shane, Rob
Dave, Kodi, Shane, Jodie, 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 mainString args
Scanner scan new ScannerSystemin;
Node head null;
whiletrue
String name scan.next;
ifnameequalsnext
head head.next;
else ifnameequalsend
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;
ifhead null
head tempNode;
else
Node previous head;
whilepreviousnext null
previous previous.next;
previous.next tempNode;
ifpreviousstudent.age tempNode.student.age previous.student.grade tempNode.student.grade
Student tempStudent previous.student;
previous.student tempNode.student;
tempNode.student tempStudent;
Node iter head;
whileiter null
System.out.printlniterstudent.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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started