Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.NoSuchElementException;
  3. import java.util.Scanner;
  4.  
  5. class SLLNode<E> {
  6. protected E element;
  7. protected SLLNode<E> succ;
  8.  
  9. public SLLNode(E elem, SLLNode<E> succ) {
  10. this.element = elem;
  11. this.succ = succ;
  12. }
  13. }
  14.  
  15. class LinkedQueue<E>{
  16. // front i rear se linkovi do prviot i posledniot jazel
  17. SLLNode<E> front, rear;
  18. int length;
  19.  
  20. public LinkedQueue() {
  21. clear();
  22. }
  23.  
  24. public void clear() {
  25. front = rear = null;
  26. length = 0;
  27. }
  28.  
  29. public boolean isEmpty () {
  30. return (length == 0);
  31. }
  32. public int size () { return length; }
  33.  
  34. public E peek () {
  35. if (front == null)
  36. throw new NoSuchElementException();
  37. return front.element;
  38. }
  39.  
  40. public void enqueue (E x) {
  41. SLLNode<E> latest = new SLLNode<E>(x, null);
  42. if (rear != null) {
  43. rear.succ = latest;
  44. rear = latest;
  45. } else
  46. front = rear = latest;
  47. length++;
  48. }
  49.  
  50. public E dequeue () {
  51. if (front != null) {
  52. E frontmost = front.element;
  53. front = front.succ;
  54. if (front == null) rear = null;
  55. length--;
  56. return frontmost;
  57. } else
  58. throw new NoSuchElementException();
  59. }
  60.  
  61. }
  62.  
  63. public class MVR {
  64.  
  65.  
  66. public static void main(String[] args) {
  67.  
  68. LinkedQueue<Gragjanin> lk = new LinkedQueue<>();
  69. LinkedQueue<Gragjanin> p = new LinkedQueue<>();
  70. LinkedQueue<Gragjanin> v = new LinkedQueue<>();
  71.  
  72. Scanner br = new Scanner(System.in);
  73.  
  74. int N = Integer.parseInt(br.nextLine());
  75. for (int i = 1; i <= N; i++) {
  76. String imePrezime = br.nextLine();
  77. int lKarta = Integer.parseInt(br.nextLine());
  78. int pasos = Integer.parseInt(br.nextLine());
  79. int vozacka = Integer.parseInt(br.nextLine());
  80. Gragjanin covek = new Gragjanin(imePrezime, lKarta, pasos, vozacka);
  81.  
  82. if(covek.licna == 1)
  83. lk.enqueue(covek);
  84. else if(covek.pasos == 1)
  85. p.enqueue(covek);
  86. else if(covek.vozacka == 1)
  87. v.enqueue(covek);
  88. }
  89. LinkedQueue<Gragjanin> gotovi = new LinkedQueue<>();
  90. while(!lk.isEmpty()) {
  91. Gragjanin g = lk.dequeue();
  92. if(g.pasos == 1)
  93. p.enqueue(g);
  94. else if (g.vozacka == 1)
  95. v.enqueue(g);
  96. else
  97. gotovi.enqueue(g);
  98. }
  99. while(!p.isEmpty()){
  100.  
  101. Gragjanin g = p.dequeue();
  102. if(g.vozacka == 1)
  103. v.enqueue(g);
  104. else
  105. gotovi.enqueue(g);
  106. }
  107.  
  108. while(!v.isEmpty()){
  109. gotovi.enqueue(v.dequeue());
  110. }
  111.  
  112. while(!gotovi.isEmpty())
  113. System.out.println(gotovi.dequeue().ime);
  114. }
  115. }
  116.  
  117. class Gragjanin{
  118. String ime;
  119. int licna;
  120. int pasos;
  121. int vozacka;
  122.  
  123. public Gragjanin(String ime, int licna, int pasos, int vozacka) {
  124. this.ime = ime;
  125. this.licna = licna;
  126. this.pasos = pasos;
  127. this.vozacka = vozacka;
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement