Advertisement
Guest User

Untitled

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