Advertisement
Guest User

Untitled

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