SashkoKlincharov

[Java][АПС] - Војска

Jan 23rd, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.17 KB | None | 0 0
  1. Војска Problem 2 (1 / 9)
  2. Пред командантот на војската наредени се сите војници и во двојно поврзана листа дадени се нивните ID-a. На командантот не му се допаѓа како се наредени војниците и решава да одбере два под-интервали од војници и да им ги замени местата, односно војниците што се наоѓаат во едниот под-интервал ќе ги смести во другиот, и обратно.
  3.  
  4. Влез: Во првиот ред даден е бројот на војници. Во вториот ред дадени се ID-то на секој од војниците. Во третиот ред дадени се два броеви, ID на првиот војник и ID на последниот војник од првиот интервал. Во четвртиот ред дадени се два броеви, ID на првиот војник и ID на последниот војник од вториот интервал.
  5.  
  6. Излез: Да се испечати новиот редослед на војниците (т.е. на нивните ID-a)
  7.  
  8. Забелешка 1: Интервалите никогаш нема да се преклопуваат и ќе содржат барем еден војник. Целата низа ќе содржи најмалку два војника. Забелешка 2: Обратете посебно внимание кога интервалите се еден до друг и кога некој од интервалите почнува од првиот војник или завршува со последниот војник.
  9.  
  10.  
  11.  
  12. Име на класата: DLLVojska
  13.  
  14. Sample input
  15. 10
  16. 1 2 3 4 5 6 7 8 9 10
  17. 1 5
  18. 6 10
  19.  
  20. Sample output
  21. 6 7 8 9 10 1 2 3 4 5
  22.  
  23.  
  24. import java.util.Scanner;
  25. import java.util.Iterator;
  26. import java.util.NoSuchElementException;
  27.  
  28. class SLL<E> {
  29. private SLLNode<E> first;
  30.  
  31. public SLL() {
  32. // Construct an empty SLL
  33. this.first = null;
  34. }
  35.  
  36. public void deleteList() {
  37. first = null;
  38. }
  39.  
  40. public int length() {
  41. int ret;
  42. if (first != null) {
  43. SLLNode<E> tmp = first;
  44. ret = 1;
  45. while (tmp.succ != null) {
  46. tmp = tmp.succ;
  47. ret++;
  48. }
  49. return ret;
  50. } else
  51. return 0;
  52.  
  53. }
  54.  
  55. @Override
  56. public String toString() {
  57. String ret = new String();
  58. if (first != null) {
  59. SLLNode<E> tmp = first;
  60. ret += tmp + " ";
  61. while (tmp.succ != null) {
  62. tmp = tmp.succ;
  63. ret += tmp + " ";
  64. }
  65. } else
  66. ret = "Prazna lista!!!";
  67. return ret;
  68. }
  69.  
  70. public void insertFirst(E o) {
  71. SLLNode<E> ins = new SLLNode<E>(o, first);
  72. first = ins;
  73. }
  74.  
  75. public void insertAfter(E o, SLLNode<E> node) {
  76. if (node != null) {
  77. SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  78. node.succ = ins;
  79. } else {
  80. System.out.println("Dadenot jazol e null");
  81. }
  82. }
  83.  
  84. public void insertBefore(E o, SLLNode<E> before) {
  85.  
  86. if (first != null) {
  87. SLLNode<E> tmp = first;
  88. if(first==before){
  89. this.insertFirst(o);
  90. return;
  91. }
  92. //ako first!=before
  93. while (tmp.succ != before)
  94. tmp = tmp.succ;
  95. if (tmp.succ == before) {
  96. SLLNode<E> ins = new SLLNode<E>(o, before);
  97. tmp.succ = ins;
  98. } else {
  99. System.out.println("Elementot ne postoi vo listata");
  100. }
  101. } else {
  102. System.out.println("Listata e prazna");
  103. }
  104. }
  105.  
  106. public void insertLast(E o) {
  107. if (first != null) {
  108. SLLNode<E> tmp = first;
  109. while (tmp.succ != null)
  110. tmp = tmp.succ;
  111. SLLNode<E> ins = new SLLNode<E>(o, null);
  112. tmp.succ = ins;
  113. } else {
  114. insertFirst(o);
  115. }
  116. }
  117.  
  118. public E deleteFirst() {
  119. if (first != null) {
  120. SLLNode<E> tmp = first;
  121. first = first.succ;
  122. return tmp.element;
  123. } else {
  124. System.out.println("Listata e prazna");
  125. return null;
  126. }
  127. }
  128.  
  129. public E delete(SLLNode<E> node) {
  130. if (first != null) {
  131. SLLNode<E> tmp = first;
  132. if(first ==node){
  133. return this.deleteFirst();
  134. }
  135. while (tmp.succ != node && tmp.succ.succ != null)
  136. tmp = tmp.succ;
  137. if (tmp.succ == node) {
  138. tmp.succ = tmp.succ.succ;
  139. return node.element;
  140. } else {
  141. System.out.println("Elementot ne postoi vo listata");
  142. return null;
  143. }
  144. } else {
  145. System.out.println("Listata e prazna");
  146. return null;
  147. }
  148.  
  149. }
  150.  
  151. public SLLNode<E> getFirst() {
  152. return first;
  153. }
  154.  
  155. public SLLNode<E> find(E o) {
  156. if (first != null) {
  157. SLLNode<E> tmp = first;
  158. while (tmp.element != o && tmp.succ != null)
  159. tmp = tmp.succ;
  160. if (tmp.element == o) {
  161. return tmp;
  162. } else {
  163. System.out.println("Elementot ne postoi vo listata");
  164. }
  165. } else {
  166. System.out.println("Listata e prazna");
  167. }
  168. return first;
  169. }
  170.  
  171. public Iterator<E> iterator () {
  172. // Return an iterator that visits all elements of this list, in left-to-right order.
  173. return new LRIterator<E>();
  174. }
  175.  
  176. // //////////Inner class ////////////
  177.  
  178. private class LRIterator<E> implements Iterator<E> {
  179.  
  180. private SLLNode<E> place, curr;
  181.  
  182. private LRIterator() {
  183. place = (SLLNode<E>) first;
  184. curr = null;
  185. }
  186.  
  187. public boolean hasNext() {
  188. return (place != null);
  189. }
  190.  
  191. public E next() {
  192. if (place == null)
  193. throw new NoSuchElementException();
  194. E nextElem = place.element;
  195. curr = place;
  196. place = place.succ;
  197. return nextElem;
  198. }
  199.  
  200. public void remove() {
  201. //Not implemented
  202. }
  203. }
  204.  
  205. public void mirror(){
  206. if (first != null) {
  207. //m=nextsucc, p=tmp,q=next
  208. SLLNode<E> tmp = first;
  209. SLLNode<E> newsucc = null;
  210. SLLNode<E> next;
  211.  
  212. while(tmp != null){
  213. next = tmp.succ;
  214. tmp.succ = newsucc;
  215. newsucc = tmp;
  216. tmp = next;
  217. }
  218. first = newsucc;
  219. }
  220.  
  221. }
  222.  
  223. public void merge (SLL<E> in){
  224. if (first != null) {
  225. SLLNode<E> tmp = first;
  226. while(tmp.succ != null)
  227. tmp = tmp.succ;
  228. tmp.succ = in.getFirst();
  229. }
  230. else{
  231. first = in.getFirst();
  232. }
  233. }
  234. }
  235.  
  236. class SLLNode<E> {
  237. protected E element;
  238. protected SLLNode<E> succ;
  239.  
  240. public SLLNode(E elem, SLLNode<E> succ) {
  241. this.element = elem;
  242. this.succ = succ;
  243. }
  244.  
  245. @Override
  246. public String toString() {
  247. return element.toString();
  248. }
  249. }
  250.  
  251. public class Test {
  252. public static void povikaj(SLL<Integer> lista,int a,int b,int c,int d) {
  253. SLLNode<Integer> tmp = lista.getFirst();
  254. SLLNode<Integer> pamtiA = null;
  255. SLLNode<Integer> pamtiC = null;
  256. SLLNode<Integer> tmp2 = null;
  257. while(tmp!=null) {
  258. if(tmp.element==a) {
  259. pamtiA = tmp; //vo pamtiA ke zastanime na node A
  260. while(tmp.element!=b) {
  261. tmp = tmp.succ; //tmp ke go imame na node B
  262. //System.out.println(tmp);
  263. }
  264. tmp2 = tmp;
  265. while(tmp2!=null) {
  266. if(tmp2.element==c) {
  267. pamtiC = tmp2; //vo pamtiC ke zastanime na node C
  268. while(tmp2.element!=d) {
  269. tmp2 = tmp2.succ; //tmp2 ke go imame na node D
  270. }
  271. }
  272. if(tmp2.element==d) {
  273. break;
  274. }
  275. tmp2 = tmp2.succ;
  276. }
  277. }
  278. if(tmp.element==b) {
  279. SLLNode<Integer> pamtiA2 = pamtiA;
  280. SLLNode<Integer> pamtiC2 = pamtiC;
  281. while(pamtiA!=tmp) {
  282. lista.insertBefore(pamtiA.element, pamtiC);
  283. pamtiA = pamtiA.succ;
  284. }
  285. lista.insertBefore(pamtiA.element, pamtiC);
  286. while(pamtiC!=tmp2) {
  287. lista.insertBefore(pamtiC.element, pamtiA2);
  288. pamtiC = pamtiC.succ;
  289. }
  290. lista.insertBefore(pamtiC.element, pamtiA2);
  291.  
  292. while(pamtiA2!=tmp) {
  293. lista.delete(pamtiA2);
  294. pamtiA2 = pamtiA2.succ;
  295. }
  296. lista.delete(pamtiA2);
  297. while(pamtiC2!=tmp2) {
  298. lista.delete(pamtiC2);
  299. pamtiC2 = pamtiC2.succ;
  300. }
  301. lista.delete(pamtiC2);
  302. break;
  303.  
  304.  
  305. }
  306.  
  307. tmp = tmp.succ;
  308. }
  309. System.out.print(lista);
  310. }
  311. public static void main(String[] args) {
  312. Scanner input = new Scanner(System.in);
  313. int n = input.nextInt();
  314. SLL<Integer> lista = new SLL<Integer>();
  315. for(int i=0;i<n;i++) {
  316. lista.insertLast(input.nextInt());
  317. }
  318. int a = input.nextInt();
  319. int b = input.nextInt();
  320. int c = input.nextInt();
  321. int d = input.nextInt();
  322.  
  323. povikaj(lista,a,b,c,d);
  324. }
  325.  
  326. }
Add Comment
Please, Sign In to add comment