Advertisement
Guest User

Untitled

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