Advertisement
Guest User

class DLLNode<E> { protected E element; protected DLLNode<

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