Advertisement
Davor97

VEZBA_APS_KOL1_z2

Nov 13th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5.  
  6. class SLLNode<E> {
  7. protected E element;
  8. protected SLLNode<E> succ;
  9.  
  10. public SLLNode(E elem, SLLNode<E> succ) {
  11. this.element = elem;
  12. this.succ = succ;
  13. }
  14.  
  15. @Override
  16. public String toString() {
  17. return element.toString();
  18. }
  19. }
  20.  
  21. class SLL<E> {
  22. private SLLNode<E> first;
  23.  
  24. public SLL() {
  25. // Construct an empty SLL
  26. this.first = null;
  27. }
  28.  
  29. public void deleteList() {
  30. first = null;
  31. }
  32.  
  33. public int length() {
  34. int ret;
  35. if (first != null) {
  36. SLLNode<E> tmp = first;
  37. ret = 1;
  38. while (tmp.succ != null) {
  39. tmp = tmp.succ;
  40. ret++;
  41. }
  42. return ret;
  43. } else
  44. return 0;
  45.  
  46. }
  47.  
  48. @Override
  49. public String toString() {
  50. String ret = new String();
  51. if (first != null) {
  52. SLLNode<E> tmp = first;
  53. ret += tmp + "->";
  54. while (tmp.succ != null) {
  55. tmp = tmp.succ;
  56. ret += tmp + "->";
  57. }
  58. } else
  59. ret = "Prazna lista!!!";
  60. return ret;
  61. }
  62.  
  63. public void insertFirst(E o) {
  64. SLLNode<E> ins = new SLLNode<E>(o, first);
  65. first = ins;
  66. }
  67.  
  68. public void insertAfter(E o, SLLNode<E> node) {
  69. if (node != null) {
  70. SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  71. node.succ = ins;
  72. } else {
  73. System.out.println("Dadenot jazol e null");
  74. }
  75. }
  76.  
  77. public void insertBefore(E o, SLLNode<E> before) {
  78.  
  79. if (first != null) {
  80. SLLNode<E> tmp = first;
  81. if(first==before){
  82. this.insertFirst(o);
  83. return;
  84. }
  85. //ako first!=before
  86. while (tmp.succ != before)
  87. tmp = tmp.succ;
  88. if (tmp.succ == before) {
  89. SLLNode<E> ins = new SLLNode<E>(o, before);
  90. tmp.succ = ins;
  91. } else {
  92. System.out.println("Elementot ne postoi vo listata");
  93. }
  94. } else {
  95. System.out.println("Listata e prazna");
  96. }
  97. }
  98.  
  99. public void insertLast(E o) {
  100. if (first != null) {
  101. SLLNode<E> tmp = first;
  102. while (tmp.succ != null)
  103. tmp = tmp.succ;
  104. SLLNode<E> ins = new SLLNode<E>(o, null);
  105. tmp.succ = ins;
  106. } else {
  107. insertFirst(o);
  108. }
  109. }
  110.  
  111. public E deleteFirst() {
  112. if (first != null) {
  113. SLLNode<E> tmp = first;
  114. first = first.succ;
  115. return tmp.element;
  116. } else {
  117. System.out.println("Listata e prazna");
  118. return null;
  119. }
  120. }
  121.  
  122. public E delete(SLLNode<E> node) {
  123. if (first != null) {
  124. SLLNode<E> tmp = first;
  125. if(first ==node){
  126. return this.deleteFirst();
  127. }
  128. while (tmp.succ != node && tmp.succ.succ != null)
  129. tmp = tmp.succ;
  130. if (tmp.succ == node) {
  131. tmp.succ = tmp.succ.succ;
  132. return node.element;
  133. } else {
  134. System.out.println("Elementot ne postoi vo listata");
  135. return null;
  136. }
  137. } else {
  138. System.out.println("Listata e prazna");
  139. return null;
  140. }
  141.  
  142. }
  143.  
  144. public SLLNode<E> getFirst() {
  145. return first;
  146. }
  147.  
  148. public SLLNode<E> find(E o) {
  149. if (first != null) {
  150. SLLNode<E> tmp = first;
  151. while (tmp.element != o && tmp.succ != null)
  152. tmp = tmp.succ;
  153. if (tmp.element == o) {
  154. return tmp;
  155. } else {
  156. System.out.println("Elementot ne postoi vo listata");
  157. }
  158. } else {
  159. System.out.println("Listata e prazna");
  160. }
  161. return first;
  162. }
  163.  
  164. }
  165.  
  166. public class PodeliSporedProsek {
  167.  
  168. public static void podeli(SLL<Integer> lista){
  169. SLL<Integer> pomali = new SLL<Integer>(), pogolemi = new SLL<Integer>();
  170.  
  171. /*
  172. *
  173. * Vasiot kod tuka
  174. *
  175. * */
  176.  
  177. //Pechatenje na pomali i ednakvi od prosekot
  178. SLLNode<Integer> pom = pomali.getFirst();
  179. while (pom!=null) {
  180. System.out.print(pom.element);
  181. if(pom.succ!=null)
  182. System.out.print(" ");
  183. pom = pom.succ;
  184. }
  185. System.out.println();
  186.  
  187. //Pechatenje na pogolemi od prosekot
  188. pom = pogolemi.getFirst();
  189. while (pom!=null) {
  190. System.out.print(pom.element);
  191. if(pom.succ!=null)
  192. System.out.print(" ");
  193. pom = pom.succ;
  194. }
  195. System.out.println();
  196.  
  197. }
  198.  
  199. public static void main(String[] args) throws IOException{
  200. SLL<Integer> lista = new SLL<Integer>();
  201. BufferedReader stdin = new BufferedReader(new InputStreamReader(
  202. System.in));
  203. String s = stdin.readLine();
  204. int N = Integer.parseInt(s);
  205. s = stdin.readLine();
  206. String[] pomniza = s.split(" ");
  207. for (int i = 0; i < N; i++) {
  208. lista.insertLast(Integer.parseInt(pomniza[i]));
  209. }
  210.  
  211. podeli(lista);
  212.  
  213. }
  214. }
  215. Sample input
  216. 5
  217. 4 2 1 5 3
  218. Sample output
  219. 2 1 3
  220. 4 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement