sunfresh

students order

Jan 28th, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. public class StudentsOrder {
  2. public static void main(String[] args) {
  3. Scanner scanner = new Scanner(System.in);
  4. int K = Integer.parseInt(scanner.nextLine().split(" ")[1]);
  5. Map<String, Node> seats = new HashMap<>();
  6. MyStudentsList studentsList = new MyStudentsList();
  7. for (String student : scanner.nextLine().split(" ")) {
  8. Node node = studentsList.addLast(student);
  9. seats.put(student, node);
  10. }
  11. for (int i = 0; i < K; i++) {
  12. String[] seatsChange = scanner.nextLine().split(" ");
  13. String stud1 = seatsChange[0];
  14. String stud2 = seatsChange[1];
  15.  
  16. Node currentSeat = seats.get(stud1);
  17. Node targetSeat = seats.get(stud2);
  18. studentsList.addBefore(currentSeat, targetSeat);
  19. }
  20.  
  21. for (String s : studentsList) {
  22. System.out.print(s + " ");
  23. }
  24. }
  25. }
  26. class Node {
  27. final String value;
  28. Node prev;
  29. Node next;
  30.  
  31. Node(String value) {
  32. this.value = value;
  33. }
  34. }
  35.  
  36. class MyStudentsList implements Iterable<String> {
  37. private Node head;
  38. private Node tail;
  39. private int size = 0;
  40.  
  41. public MyStudentsList(){
  42.  
  43. }
  44.  
  45. public Node addLast(String name) {
  46. Node newNode = new Node(name);
  47. if(size == 0){
  48. head = tail = newNode;
  49. } else{
  50. newNode.prev = tail;
  51. tail.next = newNode;
  52. tail = newNode;
  53. }
  54. size++;
  55. return tail;
  56. }
  57.  
  58. public void addBefore(Node currentSeat, Node targetSeat) {
  59. if (currentSeat.next == targetSeat){
  60. return;
  61. } else if (currentSeat == head){
  62. removeFirst();
  63. } else if (currentSeat == tail){
  64. removeLast();
  65. } else {
  66. currentSeat.prev.next = currentSeat.next;
  67. currentSeat.next.prev = currentSeat.prev;
  68. }
  69.  
  70. if (targetSeat == head){
  71. insertFirst(currentSeat);
  72. return;
  73. }
  74.  
  75. targetSeat.prev.next = currentSeat;
  76. currentSeat.prev = targetSeat.prev;
  77.  
  78. currentSeat.next = targetSeat;
  79. targetSeat.prev = currentSeat;
  80. }
  81.  
  82. private void insertFirst(Node node) {
  83. node.next = head;
  84. head.prev = node;
  85. head = node;
  86. }
  87.  
  88. private void removeFirst(){
  89. head = head.next;
  90. head.prev = null;
  91. }
  92.  
  93. private void removeLast(){
  94. tail = tail.prev;
  95. tail.next = null;
  96. }
  97.  
  98. public int size() {
  99. return size;
  100. }
  101.  
  102. @Override
  103. public Iterator<String> iterator() {
  104. return new Iterator<String>() {
  105. Node current = head;
  106. @Override
  107. public boolean hasNext() {
  108. return current != null;
  109. }
  110.  
  111. @Override
  112. public String next() {
  113. String value = current.value;
  114. current = current.next;
  115. return value;
  116. }
  117. };
  118. }
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment