Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.05 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.  
  50. System.out.println("\nStop reading a file: \n");
  51.  
  52. } else {
  53. System.out.println("Error: file empty, or does't not found, or there are IO errors: \n");
  54. }
  55.  
  56. System.out.println(System.lineSeparator());
  57. System.out.println("THE FIRST LIST:");
  58. System.out.printf("Size = %d\n", size(firstList));
  59. printList(firstList);
  60.  
  61. // Task 3.2:Change the first double-linked list and create new single-linked list
  62. SLNode secondList = createSecondList(firstList);
  63.  
  64. // print the second list
  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("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. private static SLNode createSLNode(int data) {
  84. SLNode newNode = new SLNode();
  85. newNode.data = data;
  86. newNode.next = null;
  87. return newNode;
  88.  
  89. }
  90.  
  91. /**
  92. * creates new node of the double-linked list
  93. *
  94. * @param data
  95. * integer number to be in the node
  96. * @return new node
  97. */
  98. private static DLNode createDLNode(int data) {
  99. DLNode newNode = new DLNode();
  100. newNode.data = data;
  101. newNode.next = null;
  102. newNode.prev = null;
  103. return newNode;
  104.  
  105. }
  106.  
  107. /**
  108. * Adds new node to the double-linked list to the position defined in the
  109. * variant. Head of list that passed as parameter can be differ from the
  110. * head when it returns if the new node is inserted to the beginning of the
  111. * list
  112. *
  113. * @param head
  114. * the first node of the double-linked list
  115. * @param node
  116. * new node to be added to the list
  117. * @return head of double-linked list
  118. */
  119. private static DLNode addNode(DLNode head, DLNode node) {
  120.  
  121. // TODO
  122. // Add implementation
  123. // task 3.1
  124. // position where new node to be insert defines by the variant - the item (a)
  125.  
  126. int min = Integer.MAX_VALUE;
  127. DLNode minnode = head;
  128. if (null == head){
  129. head = node;
  130. } else {
  131. DLNode curr = head;
  132. while (curr != null && curr.data != min) {
  133. if (curr.data < min) {
  134. minnode = curr;
  135. min = curr.data;
  136. }
  137. curr = curr.next;
  138. }
  139. /* while (curr != null && curr.data != min) {
  140. curr = curr.next;
  141. }*/
  142.  
  143. node.next = minnode.next;
  144. node.prev = minnode;
  145.  
  146. if (minnode.next != null) {
  147. minnode.next.prev = node;
  148. }
  149. minnode.next = node;
  150. }
  151.  
  152. return head;
  153. }
  154.  
  155.  
  156. /**
  157. * adds new node to the single-linked list to the position defined in the
  158. * variant. Head of list that passed as parameter can be differ from the
  159. * head when it returns if the new node is inserted to the beginning of the
  160. * list
  161. * @param head
  162. * the first node of the single-linked list
  163. *
  164. * @param node
  165. * new node to be added to the list
  166. * @return
  167. * head of single-linked list
  168. */
  169. private static SLNode addNode(SLNode head, SLNode node) {
  170. // TODO
  171. // Add implementation
  172. // task 3.2
  173. // position where new node to be insert defines by the variant - the item (c)
  174. SLNode mid = mid(head);
  175. if (head == null){
  176. head = node;
  177. } else {
  178. mid = head;
  179. while (mid.next != null){
  180. mid = mid.next;
  181. }
  182. mid.next = node;
  183. }
  184.  
  185.  
  186. return head;
  187. }
  188.  
  189. /**
  190. * prints values from nodes in the single-linked list if size of list equals
  191. * 0, print message "The list is empty"
  192. *
  193. * @param firstList
  194. * head of single-linked list
  195. */
  196. private static void printList(SLNode firstList) {
  197. // TODO
  198. // Add implementation
  199. // if the list is empty, print a message
  200. // if the list is not empty, print all numbers one by one in the line
  201. if(firstList == null) {
  202. System.out.println("\nThe list is empty");
  203. } else{
  204. SLNode curr = firstList;
  205. while (curr != null){
  206. System.out.printf("%5d", curr.data);
  207. curr = curr.next;
  208. }
  209. }
  210. System.out.println();
  211.  
  212. }
  213.  
  214. /**
  215. * prints values from nodes in the double-linked list. If size of list equals
  216. * 0, print message "The list is empty"
  217. *
  218. * @param list
  219. * head of double-linked list
  220. */
  221.  
  222. private static void printList(DLNode list) {
  223. // TODO
  224. // Add implementation
  225. // if the list is empty, print a message
  226. // if the list is not empty, print all numbers one by one in the line
  227. if(list == null) {
  228. System.out.println("\nThe list is empty");
  229. } else{
  230. DLNode curr = list;
  231. while (curr != null){
  232. System.out.printf("%5d", curr.data);
  233. curr = curr.next;
  234. }
  235. }
  236. System.out.println();
  237. }
  238.  
  239. /**obtains the number of nodes in the list
  240. * @param list
  241. * head of single-linked list
  242. * @return number of nodes in the list or 0, if the list is empty
  243. */
  244. private static int size(SLNode list) {
  245. // TODO
  246. // Add implementation
  247.  
  248. int count = 0;
  249. while (list != null){
  250. count ++;
  251. list = list.next;
  252. }
  253. return count;
  254. }
  255.  
  256. /**
  257. * obtains the nodes of nodes in the list
  258. * @param list
  259. * head of double-linked list
  260. * @return number of nodes in the list or 0, if the list is empty
  261. */
  262. private static int size(DLNode list) {
  263. // TODO
  264. // Add implementation
  265. if (list != null){
  266. int count = 0;
  267. while (list != null){
  268. count ++;
  269. list = list.next;
  270. }
  271. return count;
  272. } else {
  273. return 0;
  274. }
  275. }
  276.  
  277. /**
  278. * finds nodes of double-linked list that satisfies given condition and
  279. * delete theirs from list. create new single-linked list that contains
  280. * nodes with values of such deleted nodes
  281. *
  282. * @param dlHead
  283. * first node of double-linked list
  284. * @return head of newly created single-linked list, or null, if any
  285. * elements of double-linked list can't be deleted
  286. */
  287. private static SLNode createSecondList(DLNode dlHead) {
  288. // TODO
  289. // task 3.2
  290. // create head of single-linked list
  291. // go through double-linked list from the head to the tail
  292. // if the current node of double-linked list is such to be deleted (see variant item (b)),
  293. // save data from this node, delete node, create new single-linked list
  294. // node with saved data and add new node to the list in the given place
  295. // (see variant item (c))
  296. SLNode head = null;
  297. DLNode x = dlHead;
  298. while (x != null) {
  299. if (x.data % 2 == 0) {
  300. dlHead = remove(dlHead,x);
  301. if (size(head) == 0) {
  302. head = addFirst(head,x.data);
  303. } else {
  304. SLNode node = createSLNode(x.data);
  305. head = addNode(head,node);
  306. }
  307. }
  308. x = x.next;
  309. }
  310. return head;
  311. }
  312.  
  313.  
  314.  
  315. private static SLNode mid(SLNode head){
  316. SLNode x = head;
  317. int count = size(head);
  318. for(int i = 0; i<count/2; i++ ){
  319. x=x.next;
  320. }
  321. return x;
  322. }
  323. private static DLNode remove(DLNode h, DLNode x) {
  324. if (x.prev != null) {
  325. x.prev.next = x.next;
  326. } else {
  327. h = h.next;
  328. firstList = h;
  329. }
  330. if (x.next != null) {
  331. x.next.prev = x.prev;
  332. }
  333. return h;
  334. }
  335. private static SLNode addFirst(SLNode head, int value) {
  336. SLNode x = createSLNode(value);
  337. x.data = value;
  338. x.next = head;
  339. return x;
  340. }
  341.  
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement