Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Надя, [20.11.17 19:33]
- public class Main {
- private static String fileName = "input.dat";
- private static String currenDir = System.getProperty("user.dir") + File.separatorChar + "data";
- private static DLNode firstList = null;
- public static void main(String[] args) {
- FileAssistant fa = new FileAssistant();
- fa.setFileName(currenDir, fileName);
- // Task 3.1: Create the first double-linked list
- if (fa.readFile()) {
- // Successful reading file
- System.out.println("Start reading a file: ");
- // read the first integer value from the text file
- int number = fa.readNextInt();
- while (number != FileAssistant.ERROR_CODE) {
- //Output integer from a file
- System.out.printf("%5d", number);
- //create new node of double-linked list
- DLNode node = createDLNode(number);
- //add new node to a double-linked list
- firstList = addNode(firstList, node);
- // read the next integer value from the text file
- number = fa.readNextInt();
- }
- System.out.println("\nStop reading a file: \n");
- } else {
- System.out.println("Error: file empty, or does't not found, or there are IO errors: \n");
- }
- System.out.println(System.lineSeparator());
- System.out.println("THE FIRST LIST:");
- System.out.printf("Size = %d\n", size(firstList));
- printList(firstList);
- // Task 3.2:Change the first double-linked list and create new single-linked list
- SLNode secondList = createSecondList(firstList);
- // print the second list
- System.out.println("THE SECOND LIST:");
- System.out.printf("Size = %d\n", size(secondList));
- printList(secondList);
- // check the content of the first list
- System.out.println("THE FIRST LIST:");
- System.out.printf("Size = %d\n", size(firstList));
- printList(firstList);
- }
- /**
- * creates new node of the single-linked list
- *
- * @param data
- * integer number to be in the node
- * @return new node
- */
- private static SLNode createSLNode(int data) {
- SLNode newNode = new SLNode();
- newNode.data = data;
- newNode.next = null;
- return newNode;
- }
- /**
- * creates new node of the double-linked list
- *
- * @param data
- * integer number to be in the node
- * @return new node
- */
- private static DLNode createDLNode(int data) {
- DLNode newNode = new DLNode();
- newNode.data = data;
- newNode.next = null;
- newNode.prev = null;
- return newNode;
- }
- /**
- * Adds new node to the double-linked list to the position defined in the
- * variant. Head of list that passed as parameter can be differ from the
- * head when it returns if the new node is inserted to the beginning of the
- * list
- *
- * @param head
- * the first node of the double-linked list
- * @param node
- * new node to be added to the list
- * @return head of double-linked list
- */
- private static DLNode addNode(DLNode head, DLNode node) {
- // TODO quest
- // Add implementation
- // task 3.1
- // position where new node to be insert defines by the variant - the item (a)
- if (head == null)
- {
- head = node;
- } else
- {
- node.next = head.next;
- node.prev = head;
- if (head.next != null)
- head.next.prev = node;
- head.next = node;
- }
- return head;
- }
- /**
- * adds new node to the single-linked list to the position defined in the
- * variant. Head of list that passed as parameter can be differ from the
- * head when it returns if the new node is inserted to the beginning of the
- * list
- * @param head
- * the first node of the single-linked list
- *
- * @param node
- * new node to be added to the list
- * @return
- * head of single-linked list
- */
- private static SLNode addNode(SLNode head, SLNode node) {
- // TODO
- // Add implementation
- // task 3.2
- // position where new node to be insert defines by the variant - the item (c)
- if (head == null)
- {
- head = node;
- } else
- {
- SLNode tail = head;
- while (tail.next != null)
- tail = tail.next;
- tail.next = node;
- }
- return head;
- Надя, [20.11.17 19:33]
- }
- /**
- * prints values from nodes in the single-linked list if size of list equals
- * 0, print message "The list is empty"
- *
- * @param firstList
- * head of single-linked list
- */
- private static void printList(SLNode list) {
- // TODO
- // Add implementation
- // if the list is empty, print a message
- // if the list is not empty, print all numbers one by one in the line
- if (list == null)
- System.out.println("List is empty");
- else
- {
- SLNode curr = list;
- while (curr != null)
- {
- System.out.print(list + " ");
- curr = curr.next;
- }
- }
- }
- /**
- * prints values from nodes in the double-linked list. If size of list equals
- * 0, print message "The list is empty"
- *
- * @param list
- * head of double-linked list
- */
- private static void printList(DLNode list) {
- // TODO
- // Add implementation
- // if the list is empty, print a message
- // if the list is not empty, print all numbers one by one in the line
- if (list == null)
- System.out.println("The list is empty");
- else
- {
- DLNode curr = list;
- while (curr != null)
- {
- System.out.print(list + " ");
- curr = curr.next;
- }
- }
- }
- /**obtains the number of nodes in the list
- * @param list
- * head of single-linked list
- * @return number of nodes in the list or 0, if the list is empty
- */
- private static int size(SLNode list) {
- // TODO
- // Add implementation
- int count = 0;
- SLNode curr = list;
- while (curr != null)
- {
- count += 1;
- curr = curr.next;
- }
- return count;
- }
- /**
- * obtains the nodes of nodes in the list
- * @param list
- * head of double-linked list
- * @return number of nodes in the list or 0, if the list is empty
- */
- private static int size(DLNode list) {
- // TODO
- // Add implementation
- int count = 0;
- DLNode curr = list;
- while (curr != null)
- {
- count += 1;
- curr = curr.next;
- }
- return count;
- }
- /**
- * finds nodes of double-linked list that satisfies given condition and
- * delete theirs from list. create new single-linked list that contains
- * nodes with values of such deleted nodes
- *
- * @param dlHead
- * first node of double-linked list
- * @return head of newly created single-linked list, or null, if any
- * elements of double-linked list can't be deleted
- */
- private static SLNode createSecondList(DLNode dlHead) {
- // TODO
- // task 3.2
- // create head of single-linked list
- // go through double-linked list from the head to the tail
- // if the current node of double-linked list is such to be deleted (see variant item (b)),
- // save data from this node, delete node, create new single-linked list
- // node with saved data and add new node to the list in the given place
- // (see variant item (c))
- DLNode curr = dlHead;
- int min = dlHead.data;
- int max = dlHead.data;
- while (curr != null)
- if (curr.data < min)
- min = curr.data;
- if (curr.data > max)
- max = curr.data;
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement