stefans

kompanija

Feb 10th, 2016
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Collections;
  5.  
  6.  
  7. class SLLNode {
  8. protected int id;
  9. protected int plata;
  10. protected SLLNode succ;
  11.  
  12. public SLLNode(int id, int plata, SLLNode succ) {
  13. this.id = id;
  14. this.plata = plata;
  15. this.succ = succ;
  16. }
  17. }
  18.  
  19. class SLL {
  20. private SLLNode first;
  21.  
  22. public SLL() {
  23. // Construct an empty SLL
  24. this.first = null;
  25. }
  26.  
  27. public void deleteList() {
  28. first = null;
  29. }
  30.  
  31. public int length() {
  32. int ret;
  33. if (first != null) {
  34. SLLNode tmp = first;
  35. ret = 1;
  36. while (tmp.succ != null) {
  37. tmp = tmp.succ;
  38. ret++;
  39. }
  40. return ret;
  41. } else
  42. return 0;
  43.  
  44. }
  45.  
  46.  
  47. public void insertFirst(int id, int plata) {
  48. SLLNode ins = new SLLNode(id, plata, first);
  49. first = ins;
  50. }
  51.  
  52. public void insertLast(int id, int plata) {
  53. if (first != null) {
  54. SLLNode tmp = first;
  55. while (tmp.succ != null)
  56. tmp = tmp.succ;
  57. SLLNode ins = new SLLNode(id, plata, null);
  58. tmp.succ = ins;
  59. } else {
  60. insertFirst(id, plata);
  61. }
  62. }
  63.  
  64. public SLLNode getFirst() {
  65. return first;
  66. }
  67.  
  68.  
  69. public SLL brisi_pomali_od(int iznos) {
  70. // Vasiot kod tuka
  71. SLLNode jazol = this.getFirst();
  72. while (jazol.succ != null&&jazol.succ.succ != null) {
  73. if (jazol.succ.plata < iznos)
  74. jazol.succ = jazol.succ.succ;
  75. else jazol = jazol.succ;
  76. }
  77. if (jazol.succ != null&&jazol.succ.plata < iznos) jazol.succ = null;
  78. else if (jazol.succ != null) jazol = jazol.succ;
  79. if (getFirst().succ != null && getFirst().plata < iznos)
  80. first = first.succ;
  81. if (getFirst().plata < iznos) {
  82. System.out.println("nema");
  83. this.deleteList();
  84. }
  85. return this;
  86. }
  87.  
  88. public SLL sortiraj_opagacki() {
  89. // Vasiot kod tuka
  90. SLLNode startFirst = getFirst();
  91. while (startFirst != null) {
  92. SLLNode j = startFirst;
  93.  
  94. while (j != null) {
  95. if (startFirst.id < j.id) {
  96. int tmp = startFirst.plata;
  97. startFirst.plata = j.plata;
  98. j.plata = tmp;
  99.  
  100. tmp = startFirst.id;
  101. startFirst.id = j.id;
  102. j.id = tmp;
  103. }
  104. j = j.succ;
  105. }
  106. startFirst = startFirst.succ;
  107. }
  108. return this;
  109. }
  110.  
  111. public void pecati(SLL lista) {
  112. SLLNode p = lista.first;
  113. while (p != null) {
  114. System.out.println(p.id + " " + p.plata);
  115. p = p.succ;
  116. }
  117. }
  118.  
  119. }
  120.  
  121. public class SLLKompanija {
  122. public static void main(String[] args) throws IOException {
  123.  
  124. SLL lista1 = new SLL();
  125. BufferedReader stdin = new BufferedReader(new InputStreamReader(
  126. System.in));
  127. String s = stdin.readLine();
  128. int N = Integer.parseInt(s);
  129.  
  130. for (int i = 0; i < N; i++) {
  131. s = stdin.readLine();
  132. String s1 = stdin.readLine();
  133. lista1.insertLast(Integer.parseInt(s), Integer.parseInt(s1));
  134. }
  135. s = stdin.readLine();
  136.  
  137. lista1 = lista1.brisi_pomali_od(Integer.parseInt(s));
  138. if (lista1 != null) {
  139. lista1 = lista1.sortiraj_opagacki();
  140. lista1.pecati(lista1);
  141. }
  142.  
  143. }
  144. }
Add Comment
Please, Sign In to add comment