Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. package sandra_sama_resavaj;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.Iterator;
  7. import java.util.NoSuchElementException;
  8.  
  9. /**
  10. *
  11. * @author Sandra
  12. */
  13. public class Sandra_sama_resavaj {
  14. public static class SLLNode<E> {
  15. protected E element;
  16. protected SLLNode<E> succ;
  17.  
  18. public SLLNode(E elem, SLLNode<E> succ) {
  19. this.element = elem;
  20. this.succ = succ;
  21. }
  22.  
  23. @Override
  24. public String toString() {
  25. return element.toString();
  26. }
  27. }
  28.  
  29. public static class SLL<E> {
  30. private SLLNode<E> first;
  31.  
  32. public SLL() {
  33. // Construct an empty SLL
  34. this.first = null;
  35. }
  36.  
  37. public void deleteList() {
  38. first = null;
  39. }
  40.  
  41. public int length() {
  42. int ret;
  43. if (first != null) {
  44. SLLNode<E> tmp = first;
  45. ret = 1;
  46. while (tmp.succ != null) {
  47. tmp = tmp.succ;
  48. ret++;
  49. }
  50. return ret;
  51. } else
  52. return 0;
  53.  
  54. }
  55.  
  56. @Override
  57. public String toString() {
  58. String ret = new String();
  59. if (first != null) {
  60. SLLNode<E> tmp = first;
  61. ret += tmp + "->";
  62. while (tmp.succ != null) {
  63. tmp = tmp.succ;
  64. ret += tmp + "->";
  65. }
  66. } else
  67. ret = "Prazna lista!!!";
  68. return ret;
  69. }
  70.  
  71. public void insertFirst(E o) {
  72. SLLNode<E> ins = new SLLNode<E>(o, first);
  73. first = ins;
  74. }
  75.  
  76. public void insertAfter(E o, SLLNode<E> node) {
  77. if (node != null) {
  78. SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  79. node.succ = ins;
  80. } else {
  81. System.out.println("Dadenot jazol e null");
  82. }
  83. }
  84.  
  85. public void insertBefore(E o, SLLNode<E> before) {
  86.  
  87. if (first != null) {
  88. SLLNode<E> tmp = first;
  89. if(first==before){
  90. this.insertFirst(o);
  91. return;
  92. }
  93. //ako first!=before
  94. while (tmp.succ != before)
  95. tmp = tmp.succ;
  96. if (tmp.succ == before) {
  97. SLLNode<E> ins = new SLLNode<E>(o, before);
  98. tmp.succ = ins;
  99. } else {
  100. System.out.println("Elementot ne postoi vo listata");
  101. }
  102. } else {
  103. System.out.println("Listata e prazna");
  104. }
  105. }
  106.  
  107. public void insertLast(E o) {
  108. if (first != null) {
  109. SLLNode<E> tmp = first;
  110. while (tmp.succ != null)
  111. tmp = tmp.succ;
  112. SLLNode<E> ins = new SLLNode<E>(o, null);
  113. tmp.succ = ins;
  114. } else {
  115. insertFirst(o);
  116. }
  117. }
  118.  
  119. public E deleteFirst() {
  120. if (first != null) {
  121. SLLNode<E> tmp = first;
  122. first = first.succ;
  123. return tmp.element;
  124. } else {
  125. System.out.println("Listata e prazna");
  126. return null;
  127. }
  128. }
  129.  
  130. public E delete(SLLNode<E> node) {
  131. if (first != null) {
  132. SLLNode<E> tmp = first;
  133. if(first ==node){
  134. return this.deleteFirst();
  135. }
  136. while (tmp.succ != node && tmp.succ.succ != null)
  137. tmp = tmp.succ;
  138. if (tmp.succ == node) {
  139. tmp.succ = tmp.succ.succ;
  140. return node.element;
  141. } else {
  142. System.out.println("Elementot ne postoi vo listata");
  143. return null;
  144. }
  145. } else {
  146. System.out.println("Listata e prazna");
  147. return null;
  148. }
  149.  
  150. }
  151.  
  152. public SLLNode<E> getFirst() {
  153. return first;
  154. }
  155.  
  156. public SLLNode<E> find(E o) {
  157. if (first != null) {
  158. SLLNode<E> tmp = first;
  159. while (tmp.element != o && tmp.succ != null)
  160. tmp = tmp.succ;
  161. if (tmp.element == o) {
  162. return tmp;
  163. } else {
  164. System.out.println("Elementot ne postoi vo listata");
  165. }
  166. } else {
  167. System.out.println("Listata e prazna");
  168. }
  169. return first;
  170. }
  171.  
  172. public Iterator<E> iterator () {
  173. // Return an iterator that visits all elements of this list, in left-to-right order.
  174. return new LRIterator<E>();
  175. }
  176.  
  177. // //////////Inner class ////////////
  178.  
  179. private class LRIterator<E> implements Iterator<E> {
  180.  
  181. private SLLNode<E> place, curr;
  182.  
  183. private LRIterator() {
  184. place = (SLLNode<E>) first;
  185. curr = null;
  186. }
  187.  
  188. public boolean hasNext() {
  189. return (place != null);
  190. }
  191.  
  192. public E next() {
  193. if (place == null)
  194. throw new NoSuchElementException();
  195. E nextElem = place.element;
  196. curr = place;
  197. place = place.succ;
  198. return nextElem;
  199. }
  200.  
  201. public void remove() {
  202. //Not implemented
  203. }
  204. }
  205.  
  206. public void mirror(){
  207. if (first != null) {
  208. //m=nextsucc, p=tmp,q=next
  209. SLLNode<E> tmp = first;
  210. SLLNode<E> newsucc = null;
  211. SLLNode<E> next;
  212.  
  213. while(tmp != null){
  214. next = tmp.succ;
  215. tmp.succ = newsucc;
  216. newsucc = tmp;
  217. tmp = next;
  218. }
  219. first = newsucc;
  220. }
  221.  
  222. }
  223.  
  224. public void merge (SLL<E> in){
  225. if (first != null) {
  226. SLLNode<E> tmp = first;
  227. while(tmp.succ != null)
  228. tmp = tmp.succ;
  229. tmp.succ = in.getFirst();
  230. }
  231. else{
  232. first = in.getFirst();
  233. }
  234. }
  235. }
  236.  
  237. public static void pecati(SLL<Integer> lista){
  238. SLLNode<Integer> tmp = lista.getFirst();
  239. while(tmp!=null){
  240. System.out.print(tmp.element);
  241. System.out.print(" ");
  242. tmp = tmp.succ;
  243. }
  244. }
  245.  
  246.  
  247. public static void PodeliSporedProsek(SLL<Integer> lista) {
  248. SLLNode<Integer> jazol1 = lista.first;
  249. SLL<Integer> lista1 = new SLL<Integer>();
  250. SLL<Integer> lista2 = new SLL<Integer>();
  251. int prosek;
  252. int suma=0, brojac=0;
  253.  
  254. while(jazol1!=null){
  255. if(jazol1.succ!=null){
  256. suma+=jazol1.element;
  257. brojac++;
  258. jazol1 = jazol1.succ;
  259. }
  260. else{
  261. suma+=jazol1.element;
  262. brojac++;
  263. break;
  264. }
  265. }
  266.  
  267. prosek = suma/brojac;
  268.  
  269. while(jazol1!=null){
  270. if (jazol1.element <= prosek) {
  271. lista1.insertLast(jazol1.element);
  272. jazol1 = jazol1.succ;
  273. }
  274.  
  275. if (jazol1.element > prosek) {
  276. lista2.insertLast(jazol1.element);
  277. jazol1 = jazol1.succ;
  278. }
  279. }
  280.  
  281. System.out.println(lista1);
  282. System.out.println(lista2);
  283.  
  284. }
  285.  
  286.  
  287.  
  288. public static void main(String[] args) throws IOException{
  289. SLL<Integer> lista = new SLL<Integer>();
  290. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  291. String s = stdin.readLine();
  292. int N = Integer.parseInt(s);
  293. s = stdin.readLine();
  294. String[] pomniza = s.split(" ");
  295. for (int i = 0; i < N; i++) {
  296. lista.insertLast(Integer.parseInt(pomniza[i]));
  297. }
  298.  
  299. PodeliSporedProsek(lista);
  300.  
  301. }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement