Advertisement
Guest User

save

a guest
Nov 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.15 KB | None | 0 0
  1. package lab3;
  2.  
  3. import java.io.File;
  4. /**
  5. * KPI- FPM - PZKS
  6. * Course: Algorithms and Data Structures (1)
  7. * Laboratory work 3
  8. * @author Olena Khomenko
  9. * This is a program skeleton for lab 3
  10. * Write your code in the places of the methods which are marked by TODO marker
  11. * Write own methods that will be invoked from existing method
  12. *
  13. */
  14.  
  15. public class Main {
  16. private static String fileName = "input.dat";
  17. private static String currenDir = System.getProperty("user.dir") + File.separatorChar + "data";
  18. private static DLNode firstList = null;
  19.  
  20. public static void main(String[] args) {
  21. FileAssistant fa = new FileAssistant();
  22.  
  23. fa.setFileName(currenDir, fileName);
  24.  
  25.  
  26. // Task 3.1: Create the first double-linked list
  27.  
  28. if (fa.readFile()) {
  29. // Successful reading file
  30.  
  31. System.out.println("Start reading a file: ");
  32.  
  33. // read the first integer value from the text file
  34. int number = fa.readNextInt();
  35.  
  36. while (number != FileAssistant.ERROR_CODE) {
  37. //Output integer from a file
  38. System.out.printf(" %d ", number);
  39.  
  40. //create new node of double-linked list
  41. DLNode node = createDLNode(number);
  42.  
  43. //add new node to a double-linked list
  44. firstList = addNode(firstList, node);
  45.  
  46. // read the next integer value from the text file
  47. number = fa.readNextInt();
  48. }
  49. System.out.println("\nStop reading a file: \n");
  50.  
  51. } else {
  52. System.out.println("Error: file empty, or does't not found, or there are IO errors: \n");
  53. }
  54.  
  55. System.out.println("THE FIRST LIST:");
  56. System.out.printf("Size = %d\n", size(firstList));
  57. printList(firstList);
  58.  
  59. // Task 3.2:Change the first double-linked list and create new single-linked list
  60. SLNode secondList = createSecondList(firstList);
  61.  
  62. // print the second list
  63. System.out.println(System.lineSeparator());
  64. System.out.println("THE SECOND LIST:");
  65. System.out.printf("Size = %d\n", size(secondList));
  66. printList(secondList);
  67.  
  68. // check the content of the first list
  69. System.out.println(System.lineSeparator());
  70. System.out.println("THE FIRST LIST:");
  71. System.out.printf("Size = %d\n", size(firstList));
  72. printList(firstList);
  73.  
  74. }
  75.  
  76. /**
  77. * creates new node of the single-linked list
  78. *
  79. * @param data
  80. * integer number to be in the node
  81. * @return new node
  82. */
  83.  
  84. private static SLNode createSLNode(int data) {
  85. SLNode newNode = new SLNode();
  86. newNode.data = data;
  87. newNode.next = null;
  88. return newNode;
  89.  
  90. }
  91.  
  92. /**
  93. * creates new node of the double-linked list
  94. *
  95. * @param data
  96. * integer number to be in the node
  97. * @return new node
  98. */
  99.  
  100. private static DLNode createDLNode(int data) {
  101. DLNode newNode = new DLNode();
  102. newNode.data = data;
  103. newNode.next = null;
  104. newNode.prev = null;
  105. return newNode;
  106.  
  107. }
  108.  
  109. /**
  110. * Adds new node to the double-linked list to the position defined in the
  111. * variant. Head of list that passed as parameter can be differ from the
  112. * head when it returns if the new node is inserted to the beginning of the
  113. * list
  114. *
  115. * @param head
  116. * the first node of the double-linked list
  117. * @param node
  118. * new node to be added to the list
  119. * @return head of double-linked list
  120. */
  121.  
  122. private static DLNode addNode(DLNode head, DLNode node) {
  123. // TODO
  124. if(head != null) {
  125. node.next = head;
  126. head.prev = node;
  127. node.prev = null;
  128. }
  129. return node;
  130. }
  131.  
  132. // Add implementation
  133. // task 3.1
  134. // position where new node to be insert defines by the variant - the item (a)
  135.  
  136. /**
  137. * adds new node to the single-linked list to the position defined in the
  138. * variant. Head of list that passed as parameter can be differ from the
  139. * head when it returns if the new node is inserted to the beginning of the
  140. * list
  141. * @param head
  142. * the first node of the single-linked list
  143. *
  144. * @param node
  145. * new node to be added to the list
  146. * @return
  147. * head of single-linked list
  148. */
  149. private static SLNode addNode(SLNode head, SLNode node) {
  150. // TODO
  151.  
  152. if(head != null) {
  153. SLNode max = head;
  154. SLNode current = head;
  155. while(current != null) {
  156. if (current.data >= max.data) {
  157. max = current;
  158. }
  159. current = current.next;
  160. }
  161. node.next = max.next;
  162. max.next = node;
  163. }
  164. else
  165. head = node;
  166. return head;
  167. }
  168.  
  169. /**
  170. * prints values from nodes in the single-linked list if size of list equals
  171. * 0, print message "The list is empty"
  172. *
  173. * @param firstList
  174. * head of single-linked list
  175. */
  176. private static void printList(SLNode list) {
  177. // TODO
  178. if(list != null) {
  179. while(list != null) {
  180. System.out.printf("%d ", list.data);
  181. list = list.next;
  182. }
  183. }
  184. System.out.println("");
  185. }
  186.  
  187. // Add implementation
  188. // if the list is empty, print a message
  189. // if the list is not empty, print all numbers one by one in the line
  190.  
  191. /**
  192. * prints values from nodes in the double-linked list. If size of list equals
  193. * 0, print message "The list is empty"
  194. *
  195. * @param list
  196. * head of double-linked list
  197. */
  198.  
  199. private static void printList(DLNode list) {
  200. // TODO
  201. if(list != null) {
  202. while(list != null) {
  203. System.out.printf("%d ", list.data);
  204. list = list.next;
  205. }
  206. }else
  207. System.out.println("List is null");
  208. System.out.println("");
  209. }
  210.  
  211. // Add implementation
  212. // if the list is empty, print a message
  213. // if the list is not empty, print all numbers one by one in the line
  214. /**obtains the number of nodes in the list
  215. * @param list
  216. * head of single-linked list
  217. * @return number of nodes in the list or 0, if the list is empty
  218. */
  219. private static int size(SLNode list) {
  220. // TODO
  221. int counter = -1;
  222. if(list != null) {
  223. counter++;
  224. SLNode current = list;
  225. while(current != null) {
  226. counter++;
  227. current = current.next;
  228. }
  229. }
  230. return counter;
  231. }
  232.  
  233. // Add implementation
  234.  
  235. /**
  236. * obtains the nodes of nodes in the list
  237. * @param list
  238. * head of double-linked list
  239. * @return number of nodes in the list or 0, if the list is empty
  240. */
  241. private static int size(DLNode list) {
  242. // TODO
  243. int counter = -1;
  244. if(list != null) {
  245. counter++;
  246. DLNode current = list;
  247. while(current != null) {
  248. counter++;
  249. current = current.next;
  250. }
  251. }
  252. return counter;
  253. // Add implementation
  254. }
  255.  
  256. /**
  257. * finds nodes of double-linked list that satisfies given condition and
  258. * delete theirs from list. create new single-linked list that contains
  259. * nodes with values of such deleted nodes
  260. *
  261. * @param dlHead
  262. * first node of double-linked list
  263. * @return head of newly created single-linked list, or null, if any
  264. * elements of double-linked list can't be deleted
  265. */
  266. private static SLNode createSecondList(DLNode dlHead) {
  267. // TODO
  268. SLNode SecondList = null;
  269. if(dlHead != null) {
  270. DLNode current = dlHead;
  271. DLNode temp = current;
  272. while(current != null) {
  273. temp = current;
  274. if (current.data % 2 == 0) {
  275. SecondList = addNode(SecondList, createSLNode(current.data));
  276. remove(dlHead, current);
  277. }
  278. current = temp.next;
  279. }
  280. }
  281. return SecondList;
  282. }
  283.  
  284. // task 3.2
  285. // create head of single-linked list
  286. // go through double-linked list from the head to the tail
  287. // if the current node of double-linked list is such to be deleted (see variant item (b)),
  288. // save data from this node, delete node, create new single-linked list
  289. // node with saved data and add new node to the list in the given place
  290. // (see variant item (c))
  291.  
  292. private static DLNode remove(DLNode dlHead, DLNode current) {
  293. System.out.printf("Ya vyklykayus ");
  294. if (current.prev == null) {
  295. dlHead = dlHead.next;
  296. }
  297. else
  298. current.prev.next = current.next;
  299.  
  300. if(current.next != null)
  301. current.next.prev = current.prev;
  302.  
  303. return dlHead;
  304. }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement