Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. package sandra_sama_resavaj2;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class Sandra_sama_resavaj2 {
  8.  
  9. public static class DLLNode<E> {
  10. protected E element;
  11. protected DLLNode<E> pred, succ;
  12.  
  13. public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  14. this.element = elem;
  15. this.pred = pred;
  16. this.succ = succ;
  17. }
  18.  
  19. @Override
  20. public String toString() {
  21. return element.toString();
  22. }
  23. }
  24.  
  25. public static class DLL<E> {
  26. private DLLNode<E> first, last;
  27.  
  28. public DLL() {
  29. // Construct an empty SLL
  30. this.first = null;
  31. this.last = null;
  32. }
  33.  
  34. public void deleteList() {
  35. first = null;
  36. last = null;
  37. }
  38.  
  39. public int length() {
  40. int ret;
  41. if (first != null) {
  42. DLLNode<E> tmp = first;
  43. ret = 1;
  44. while (tmp.succ != null) {
  45. tmp = tmp.succ;
  46. ret++;
  47. }
  48. return ret;
  49. } else
  50. return 0;
  51.  
  52. }
  53.  
  54. public DLLNode<E> find(E o) {
  55. if (first != null) {
  56. DLLNode<E> tmp = first;
  57. while (tmp.element != o && tmp.succ != null)
  58. tmp = tmp.succ;
  59. if (tmp.element == o) {
  60. return tmp;
  61. } else {
  62. System.out.println("Elementot ne postoi vo listata");
  63. }
  64. } else {
  65. System.out.println("Listata e prazna");
  66. }
  67. return first;
  68. }
  69.  
  70. public void insertFirst(E o) {
  71. DLLNode<E> ins = new DLLNode<E>(o, null, first);
  72. if (first == null)
  73. last = ins;
  74. else
  75. first.pred = ins;
  76. first = ins;
  77. }
  78.  
  79. public void insertLast(E o) {
  80. if (first == null)
  81. insertFirst(o);
  82. else {
  83. DLLNode<E> ins = new DLLNode<E>(o, last, null);
  84. last.succ = ins;
  85. last = ins;
  86. }
  87. }
  88.  
  89. public void insertAfter(E o, DLLNode<E> after) {
  90. if(after==last){
  91. insertLast(o);
  92. return;
  93. }
  94. DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  95. after.succ.pred = ins;
  96. after.succ = ins;
  97. }
  98.  
  99. public void insertBefore(E o, DLLNode<E> before) {
  100. if(before == first){
  101. insertFirst(o);
  102. return;
  103. }
  104. DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  105. before.pred.succ = ins;
  106. before.pred = ins;
  107. }
  108.  
  109. public E deleteFirst() {
  110. if (first != null) {
  111. DLLNode<E> tmp = first;
  112. first = first.succ;
  113. if (first != null) first.pred = null;
  114. if (first == null)
  115. last = null;
  116. return tmp.element;
  117. } else
  118. return null;
  119. }
  120.  
  121. public E deleteLast() {
  122. if (first != null) {
  123. if (first.succ == null)
  124. return deleteFirst();
  125. else {
  126. DLLNode<E> tmp = last;
  127. last = last.pred;
  128. last.succ = null;
  129. return tmp.element;
  130. }
  131. }
  132. // else throw Exception
  133. return null;
  134. }
  135.  
  136. public E delete(DLLNode<E> node) {
  137. if(node==first){
  138. deleteFirst();
  139. return node.element;
  140. }
  141. if(node==last){
  142. deleteLast();
  143. return node.element;
  144. }
  145. node.pred.succ = node.succ;
  146. node.succ.pred = node.pred;
  147. return node.element;
  148.  
  149. }
  150.  
  151. @Override
  152. public String toString() {
  153. String ret = new String();
  154. if (first != null) {
  155. DLLNode<E> tmp = first;
  156. ret += tmp + "<->";
  157. while (tmp.succ != null) {
  158. tmp = tmp.succ;
  159. ret += tmp + "<->";
  160. }
  161. } else
  162. ret = "Prazna lista!!!";
  163. return ret;
  164. }
  165.  
  166. public String toStringR() {
  167. String ret = new String();
  168. if (last != null) {
  169. DLLNode<E> tmp = last;
  170. ret += tmp + "<->";
  171. while (tmp.pred != null) {
  172. tmp = tmp.pred;
  173. ret += tmp + "<->";
  174. }
  175. } else
  176. ret = "Prazna lista!!!";
  177. return ret;
  178. }
  179.  
  180. public DLLNode<E> getFirst() {
  181. return first;
  182. }
  183.  
  184. public DLLNode<E> getLast() {
  185.  
  186. return last;
  187. }
  188.  
  189. public void izvadiDupliIPrebroj(){
  190.  
  191. }
  192. }
  193.  
  194. public static void pecati(DLL<Integer> lista) {
  195. DLLNode<Integer> tmp = lista.getFirst();
  196. while (tmp != null) {
  197. System.out.print(tmp.element);
  198. System.out.print(" ");
  199. tmp = tmp.succ;
  200. }
  201. }
  202.  
  203.  
  204.  
  205.  
  206. public static void PodeliListaProsek(DLL<Integer> lista){
  207. DLLNode<Integer> jazol1 = lista.getFirst();
  208. DLL<Integer> lista1 = new DLL<Integer>(), lista2 = new DLL<Integer>();
  209. int prosek=0, suma=0, brojac=0;
  210.  
  211. while(jazol1!=null){
  212. if(jazol1.succ!=null){
  213. suma+=jazol1.element;
  214. brojac++;
  215. jazol1 = jazol1.succ;
  216. }
  217. else{
  218. suma+=jazol1.element;
  219. brojac++;
  220. break;
  221. }
  222. }
  223. prosek = suma/brojac;
  224.  
  225. jazol1 = lista.getLast();
  226.  
  227. while(jazol1!=null){
  228. if(jazol1.element <= prosek){
  229. lista1.insertLast(jazol1.element);
  230. jazol1 = jazol1.pred;
  231. }
  232. else if(jazol1.element > prosek){
  233. lista2.insertLast(jazol1.element);
  234. jazol1 = jazol1.pred;
  235. }
  236. }
  237.  
  238. pecati(lista1);
  239. System.out.println("\n");
  240. pecati(lista2);
  241.  
  242. }
  243.  
  244. public static void main(String[] args) throws IOException {
  245. DLL<Integer> lista = new DLL<Integer>();
  246. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  247. String s = stdin.readLine();
  248. int N = Integer.parseInt(s);
  249.  
  250. s = stdin.readLine();
  251. String[] pomniza = s.split(" ");
  252.  
  253. for (int i = 0; i < N; i++) {
  254. lista.insertLast(Integer.parseInt(pomniza[i]));
  255. }
  256.  
  257. PodeliListaProsek(lista);
  258. }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement