Advertisement
Guest User

Untitled

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