Advertisement
Guest User

Labka-jabka

a guest
Nov 20th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.96 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("%5d", 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(System.lineSeparator());
  56. System.out.println("THE FIRST LIST:");
  57. System.out.printf("Size = %d\n", size(firstList));
  58. printList(firstList);
  59.  
  60. // Task 3.2:Change the first double-linked list and create new single-linked list
  61. SLNode secondList = createSecondList(firstList);
  62.  
  63. // print the second list
  64. System.out.println(System.lineSeparator());
  65. System.out.println("THE SECOND LIST:");
  66. System.out.printf("Size = %d\n", size(secondList));
  67. printList(secondList);
  68.  
  69. // check the content of the first list
  70. System.out.println(System.lineSeparator());
  71. System.out.println("THE FIRST LIST:");
  72. System.out.printf("Size = %d\n", size(firstList));
  73. printList(firstList);
  74.  
  75. }
  76.  
  77. /**
  78. * creates new node of the single-linked list
  79. *
  80. * @param data
  81. * integer number to be in the node
  82. * @return new node
  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. private static DLNode createDLNode(int data) {
  100. DLNode newNode = new DLNode();
  101. newNode.data = data;
  102. newNode.next = null;
  103. newNode.prev = null;
  104. return newNode;
  105.  
  106. }
  107.  
  108. /**
  109. * Adds new node to the double-linked list to the position defined in the
  110. * variant. Head of list that passed as parameter can be differ from the
  111. * head when it returns if the new node is inserted to the beginning of the
  112. * list
  113. *
  114. * @param head
  115. * the first node of the double-linked list
  116. * @param node
  117. * new node to be added to the list
  118. * @return head of double-linked list
  119. */
  120. private static DLNode addNode(DLNode head, DLNode node) {
  121.  
  122. // TODO
  123. if(head != null) {
  124. node.next = head;
  125. head.prev = node;
  126. node.prev = null;
  127. }
  128. // Add implementation
  129. // task 3.1
  130. // position where new node to be insert defines by the variant - the item (a)
  131. return node;
  132. }
  133.  
  134.  
  135. /**
  136. * adds new node to the single-linked list to the position defined in the
  137. * variant. Head of list that passed as parameter can be differ from the
  138. * head when it returns if the new node is inserted to the beginning of the
  139. * list
  140. * @param head
  141. * the first node of the single-linked list
  142. *
  143. * @param node
  144. * new node to be added to the list
  145. * @return
  146. * head of single-linked list
  147. */
  148. private static SLNode addNode(SLNode head, SLNode node) {
  149. // TODO
  150. if(head != null) {
  151. node.next = head;
  152. }
  153. // Add implementation
  154. // task 3.2
  155. // position where new node to be insert defines by the variant - the item (c)
  156. return node;
  157. }
  158.  
  159. /**
  160. * prints values from nodes in the single-linked list if size of list equals
  161. * 0, print message "The list is empty"
  162. *
  163. * @param firstList
  164. * head of single-linked list
  165. */
  166. private static void printList(SLNode list) {
  167. // TODO
  168. if(list != null) {
  169. while(list != null) {
  170. System.out.printf("%d ", list.data);
  171. list = list.next;
  172. }
  173. }
  174. System.out.println("");
  175. }
  176.  
  177. // Add implementation
  178. // if the list is empty, print a message
  179. // if the list is not empty, print all numbers one by one in the line
  180.  
  181. /**
  182. * prints values from nodes in the double-linked list. If size of list equals
  183. * 0, print message "The list is empty"
  184. *
  185. * @param list
  186. * head of double-linked list
  187. */
  188.  
  189. private static void printList(DLNode list) {
  190. // TODO
  191. if(list != null) {
  192. while(list != null) {
  193. System.out.printf("%d ", list.data);
  194. list = list.next;
  195. }
  196. }else
  197. System.out.println("List is null");
  198. System.out.println("");
  199. }
  200. // Add implementation
  201. // if the list is empty, print a message
  202. // if the list is not empty, print all numbers one by one in the line
  203. /**obtains the number of nodes in the list
  204. * @param list
  205. * head of single-linked list
  206. * @return number of nodes in the list or 0, if the list is empty
  207. */
  208. private static int size(SLNode list) {
  209. // TODO
  210. int counter = -1;
  211. if(list != null) {
  212. counter++;
  213. SLNode current = list;
  214. while(current != null) {
  215. counter++;
  216. current = current.next;
  217. }
  218. }
  219. return counter;
  220. // Add implementation
  221. }
  222.  
  223. /**
  224. * obtains the nodes of nodes in the list
  225. * @param list
  226. * head of double-linked list
  227. * @return number of nodes in the list or 0, if the list is empty
  228. */
  229. private static int size(DLNode list) {
  230. // TODO
  231. int counter = -1;
  232. if(list != null) {
  233. counter++;
  234. DLNode current = list;
  235. while(current != null) {
  236. counter++;
  237. current = current.next;
  238. }
  239. }
  240. return counter;
  241. // Add implementation
  242. }
  243.  
  244. /**
  245. * finds nodes of double-linked list that satisfies given condition and
  246. * delete theirs from list. create new single-linked list that contains
  247. * nodes with values of such deleted nodes
  248. *
  249. * @param dlHead
  250. * first node of double-linked list
  251. * @return head of newly created single-linked list, or null, if any
  252. * elements of double-linked list can't be deleted
  253. */
  254. private static SLNode createSecondList(DLNode dlHead) {
  255. // TODO
  256. SLNode SecondList = null;
  257. if(dlHead != null) {
  258. DLNode current = dlHead;
  259. while(current != null) {
  260. if (current.data % 2 == 0) {
  261. SecondList = addNode(SecondList, createSLNode(current.data));
  262. remove(dlHead, current);
  263. }
  264. current = current.next;
  265. }
  266. }
  267. return SecondList;
  268. }
  269.  
  270. // task 3.2
  271. // create head of single-linked list
  272. // go through double-linked list from the head to the tail
  273. // if the current node of double-linked list is such to be deleted (see variant item (b)),
  274. // save data from this node, delete node, create new single-linked list
  275. // node with saved data and add new node to the list in the given place
  276. // (see variant item (c))
  277. private static DLNode remove(DLNode dlHead, DLNode current) {
  278. if (current.prev != null)
  279. current.prev.next = current.next;
  280. else
  281. dlHead = dlHead.next;
  282.  
  283. if(current.next != null)
  284. current.next.prev = current.prev;
  285.  
  286. return dlHead;
  287. }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement