Advertisement
Guest User

Untitled

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