Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.33 KB | None | 0 0
  1. //Yurii Piets - nr 6
  2. import java.util.Scanner;
  3. class Wagon{
  4. private String name;
  5. private Wagon nextWagon;
  6. private Wagon prevWagon;
  7. public Wagon(){
  8. nextWagon = null;
  9. prevWagon = null;
  10. }
  11. public Wagon(String n){
  12. name = n;
  13. nextWagon = null;
  14. prevWagon = null;
  15. }
  16. public String getNameW(){
  17. return name;
  18. }
  19. public void setNextW(Wagon n){
  20. nextWagon = n;
  21. }
  22. public Wagon getNextW(){
  23. return nextWagon;
  24. }
  25. public void setPrevW(Wagon p){
  26. prevWagon = p;
  27. }
  28. public Wagon getPrevW(){
  29. return prevWagon;
  30. }
  31. }
  32. class Pociag{
  33. public Pociag Trains;
  34. private String name;
  35. private Pociag nextPociag;
  36. public Wagon firstWagon;
  37. public Wagon lastWagon;
  38.  
  39. public Pociag(){
  40. Trains = null;
  41. nextPociag = null;
  42. firstWagon = null;
  43. lastWagon = null;
  44. }
  45. public Pociag (String nameP, String nameW){
  46. name = nameP;
  47. firstWagon = new Wagon(nameW);
  48. nextPociag = null;
  49. lastWagon = firstWagon;
  50. }
  51. /*---------------------------допоміжні методи-----------------------------------------------------*/
  52. public boolean isEmpty(){
  53. return Trains==null;
  54. }
  55. public Pociag locate(String key){
  56. Pociag current = Trains;
  57. while(current != null && !(current.getName().equals(key))){
  58. current = current.getNext();
  59. }
  60. return current;
  61. }
  62. public Pociag locatePrevious(String key){
  63. Pociag current = Trains, previous = Trains;
  64. while(current != null && !(current.getName().equals(key))){
  65. previous = current;
  66. current = current.getNext();
  67. }
  68. return previous;
  69. }
  70. public void deletePociag(String key){
  71. Pociag current = locate(key);
  72. Pociag previous = locatePrevious(key);
  73. if(current == Trains) Trains = Trains.getNext(); // Если первый элеменn изменить first
  74. else previous.setNext(current.getNext());
  75. }
  76. public void add(String nP2, String nW){
  77. if(isEmpty()) Trains = new Pociag(nP2,nW);
  78. else{
  79. Pociag current = new Pociag(nP2,nW);
  80. current.setNext(Trains);
  81. Trains = current;
  82. }
  83. }
  84. /*------------------------------------------------------------------------------------------------*/
  85. public String getName(){
  86. return name;
  87. }
  88. public void setNext(Pociag p){
  89. nextPociag = p;
  90. }
  91. public Pociag getNext(){
  92. return nextPociag;
  93. }
  94. public void insertFirst(String n){
  95. Wagon newWagon = new Wagon(n);
  96. newWagon.setPrevW(null);
  97. newWagon.setNextW(firstWagon);
  98. //if(firstWagon.getPrevW() == null) firstWagon.setPrevW(newWagon);
  99. //else if (firstWagon.getNextW() == null) firstWagon.setNextW(newWagon);
  100. if(firstWagon.getPrevW() == null && firstWagon.getNextW() == null){
  101. firstWagon.setNextW(newWagon);
  102. }
  103. else if(firstWagon.getPrevW() == null) firstWagon.setPrevW(newWagon);
  104. else if (firstWagon.getNextW() == null) firstWagon.setNextW(newWagon);
  105. firstWagon = newWagon;
  106. }
  107. public void insertLast(String n){
  108. Wagon newWagon = new Wagon(n);
  109. newWagon.setNextW(null);
  110. newWagon.setPrevW(lastWagon);
  111. if(lastWagon.getNextW() == null) lastWagon.setNextW(newWagon);
  112. else if(lastWagon.getPrevW() == null) lastWagon.setPrevW(newWagon);
  113. lastWagon = newWagon;
  114. }
  115. public void display(){
  116. boolean flag = true;
  117. Wagon newWagon = firstWagon;
  118. String s = getName() + ":";
  119. while(newWagon != lastWagon){
  120. s = s + " " + newWagon.getNameW();
  121. if(newWagon.getPrevW() == null){
  122. if(newWagon == newWagon.getNextW().getPrevW()) flag = true;
  123. else flag = false;
  124. newWagon = newWagon.getNextW();
  125. }
  126. else if(newWagon.getNextW() == null){
  127. if(newWagon == newWagon.getPrevW().getNextW()) flag = false;
  128. else flag = true;
  129. newWagon = newWagon.getPrevW();
  130. }
  131. else if(flag == true){
  132. if(newWagon == newWagon.getNextW().getPrevW()) flag = true;
  133. else flag = false;
  134. newWagon = newWagon.getNextW();
  135. }
  136. else{
  137. if(newWagon == newWagon.getPrevW().getNextW()) flag = false;
  138. else flag = true;
  139. newWagon = newWagon.getPrevW();
  140. }
  141.  
  142. }
  143. System.out.println(s + " " + lastWagon.getNameW());
  144. }
  145. public void trainsList(){
  146. Pociag current = Trains;
  147. String s = "Trains:";
  148. while (current != null){
  149. s = s + " " + current.getName();
  150. current = current.nextPociag;
  151. }
  152. System.out.println(s);
  153. }
  154. public void delFirst(){
  155. //if(firstWagon != null)
  156. if(firstWagon.getNextW() == null){
  157. if(firstWagon.getPrevW().getPrevW() == firstWagon) firstWagon.getPrevW().setPrevW(null);
  158. else if(firstWagon.getPrevW().getNextW() == firstWagon) firstWagon.getPrevW().setNextW(null);
  159. firstWagon = firstWagon.getPrevW();
  160. }
  161. else if(firstWagon.getPrevW() == null){
  162. if(firstWagon.getNextW().getNextW() == firstWagon) firstWagon.getNextW().setNextW(null);
  163. else if(firstWagon.getNextW().getPrevW() == firstWagon) firstWagon.getNextW().setPrevW(null);
  164. firstWagon = firstWagon.getNextW();
  165. }
  166. }
  167. public void delLast(){
  168. //if(lastWagon != null)
  169. if(lastWagon.getNextW() == null){
  170. if(lastWagon.getPrevW().getNextW() == lastWagon) lastWagon.getPrevW().setNextW(null);
  171. else if(lastWagon.getPrevW().getPrevW() == lastWagon) lastWagon.getPrevW().setPrevW(null);
  172. lastWagon = lastWagon.getPrevW();
  173. }
  174. else if(lastWagon.getPrevW() == null){
  175. if(lastWagon.getNextW().getPrevW() == lastWagon) lastWagon.getNextW().setPrevW(null);
  176. else if(lastWagon.getNextW().getNextW() == lastWagon) lastWagon.getNextW().setNextW(null);
  177. lastWagon = lastWagon.getNextW();
  178. }
  179. }
  180. public void reverse(){
  181. Wagon buff = firstWagon;
  182. firstWagon = lastWagon;
  183. lastWagon = buff;
  184. }
  185. public void union(Pociag p2){
  186. if(lastWagon.getNextW() == null){ //перший нормальний
  187. lastWagon.setNextW(p2.firstWagon);
  188. if(p2.firstWagon.getPrevW() == null) p2.firstWagon.setPrevW(lastWagon); //другий нормальний
  189. else if( p2.firstWagon.getNextW() == null) p2.firstWagon.setNextW(lastWagon); //другий розвернутий
  190. }
  191. else{ if(lastWagon.getPrevW() == null ){ //перший ровернуий
  192. lastWagon.setPrevW(p2.firstWagon);
  193. if(p2.firstWagon.getPrevW() == null) p2.firstWagon.setPrevW(lastWagon); //другий нормальний
  194. else if(p2.firstWagon.getNextW() == null) p2.firstWagon.setNextW(lastWagon); //другий розвенруний
  195. }
  196. }
  197. lastWagon = p2.lastWagon;
  198. }
  199. }
  200. public class Source{
  201. public static Scanner inScan = new Scanner(System.in);
  202. public static void main(String[] args){
  203. //Scanner inScan = new Scanner(System.in);
  204. Integer liczZ = inScan.nextInt();
  205. while(liczZ-- > 0){
  206. //for(int i = 0 ; i < liczZ ; i++){
  207. String nP = "", nW = "", nP1="", nP2="";
  208. Pociag p = new Pociag();
  209. Integer liczN = inScan.nextInt();
  210. while(liczN -- > 0){
  211. //for(int j = 0; j < liczN; j++){
  212. String Polecenie = inScan.next();
  213. /*----------------------------New------------------------------*/
  214. if ( Polecenie.equals("New") ){
  215. nP = inScan.next();
  216. nW = inScan.next();
  217. p.add(nP,nW);
  218. //if( p.isEmpty() ) p.Trains = new Pociag(nP,nW);
  219. //else p.add(nP,nW);
  220. }
  221. /*------------------------InsertFirst--------------------------*/
  222. else if (Polecenie.equals("InsertFirst")){
  223. nP = inScan.next();
  224. nW = inScan.next();
  225. Pociag newP = p.locate(nP);
  226. //if(newP != null)
  227. newP.insertFirst(nW);
  228. }
  229. /*------------------------InsertLast--------------------------*/
  230. else if (Polecenie.equals("InsertLast")){
  231. nP = inScan.next();
  232. nW = inScan.next();
  233. Pociag newP = p.locate(nP);
  234. //if(newP != null)
  235. newP.insertLast(nW);
  236. }
  237. /*--------------------------Display----------------------------*/
  238. else if( Polecenie.equals("Display")){
  239. nP = inScan.next();
  240. Pociag newP = p.locate(nP);
  241. //if(newP != null)
  242. newP.display();
  243. }
  244. /*--------------------- -----TrainsList----------------------------*/
  245. else if (Polecenie.equals("TrainsList") ){
  246. p.trainsList();
  247. }
  248. /*--------------------------Reverse----------------------------*/
  249. else if (Polecenie.equals("Reverse")){
  250. nP = inScan.next();
  251. Pociag newP = p.locate(nP);
  252. //if(newP != null)
  253. newP.reverse();
  254. }
  255. /*------------------------------------------Union-----------------------------------------*/
  256. else if (Polecenie.equals("Union")){
  257. nP1 = inScan.next();
  258. nP2 = inScan.next();
  259. Pociag p1 = p.locate(nP1);
  260. Pociag p2 = p.locate(nP2);
  261. //if(p1 != null && p2 != null)
  262. p1.union(p2);
  263. p.deletePociag(nP2);
  264. }
  265. /*------------------------------------------DelFirst-----------------------------------------*/
  266. else if (Polecenie.equals("DelFirst")){
  267. nP1 = inScan.next();
  268. nP2 = inScan.next();
  269. Pociag current = p.locate(nP1);
  270. //if(current != null){
  271. nW = current.firstWagon.getNameW();
  272. if(current.firstWagon.getNextW() == null && current.firstWagon.getPrevW() == null) p.deletePociag(nP1);
  273. else current.delFirst();
  274. p.add(nP2, nW);
  275. // }
  276. }
  277. /*------------------------------------------DelLast--------------------------------------------------*/
  278. else if (Polecenie.equals("DelLast")){
  279. nP1 = inScan.next();
  280. nP2 = inScan.next();
  281. Pociag current = p.locate(nP1);
  282. // if(current != null){
  283. nW = current.lastWagon.getNameW();
  284. if(current.lastWagon.getPrevW() == null && current.lastWagon.getNextW() == null) p.deletePociag(nP1);
  285. else current.delLast();
  286. p.add(nP2, nW);
  287. // }
  288.  
  289. }
  290. }
  291. }
  292. }
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement