Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1: reate a new Java Application to manage Linked Lists: (Note: Do not us

ID: 3722428 • Letter: P

Question

Problem 1: reate a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList) a) Create a DateTime class 1. Add day, month, year, hours, minutes as attributes 2. Add a constructor and a toString() method 3. Implement the Comparable interface, and add a CompareTo() method 4. Add methods to get and set all attributes b) Add to MyLinkedList class the following methods: 1. Insert a Node to a particular position in the List 2. Insert a Node in place, assuming that the list is ordered in ascending order. 3. Delete node in front 4. Delete last node 5. Delete a node given its position 6. Method to find the node with the minimum value in the list -getMinimum) 7. Method to merge two lists together and return the resulting linked list 8. Sort list in ascending order 9. Sort list in descending order c) Create a Test3 class to do the following in the main method: 1. Create a new List (call it dateTimeList) of type MyLinkedList 2. Insert five DateTime objects numbers to a specific position in the list (not ordere 3. Display all the DateTime objects in the List 4. Display the DateTime objects occurring after today 5. Insert a new DateTime elements to its appropriate position in the List and displa 6. Sort the List and display it in both ascending and descending 7. Display the smallest and the largest elements in the list 8. Display all DateTime objects with the time in the afternoon 9. Delete a DateTime object from the front of the list 10. Delete a DateTime object from the back of the list 11. Create two lists of DateTime objects, merge them together, and then print the r

Explanation / Answer

Answer:-

Question (A):-

Here I created a java application to manage linked lists:-

public class DateTime implements Comparable<DateTime> {

    public int day;

    public int year;

    public int months;

    public int minutes;

    public int hours;

    DateTime(int minutes, int hours, int day, int months, int year) {

        this.day = day;

        this.hours = hours;

        this.year = year;

        this.months = months;

        this.minutes = minutes;

    }

    public String toString() {

        return minutes + " " + hours + " " + months + " " + day + " " + year;

    }

    public static void main(String[] args) {

        DateTime dd = new DateTime(12, 02, 07, 02, 2018);

        System.out.println(dd);

    }

    public int getDay() {

        return day;

    }

    public int getYear() {

        return year;

    }

    public int getMonths() {

        return months;

    }

    public int getMinutes() {

        return minutes;

    }

    public int getHours() {

        return hours;

    }

    public void setDay(int day) {

        this.day = day;

    }

    public void setYear(int year) {

        this.year = year;

    }

    public void setMonths(int months) {

        this.months = months;

    }

    public void setHours(int hours) {

        this.hours = hours;

    }

    public void setMinutes(int minutes) {

        this.minutes = minutes;

    }

    @Override

    public int compareTo(DateTime dt) {

        if (day == dt.day)

            return 0;

        else if (day > dt.day)

            return 1;

        else

            return -1;

        if (hours == dt.hours)

            return 0;

        else if (hours > dt.hours)

            return 1;

        else

            return -1;

        if (year == dt.year)

            return 0;

        else if (year > dt.year)

            return 1;

        else

            return -1;

        if (months == dt.months)

            return 0;

        else if (months > dt.months)

            return 1;

        else

            return -1;

        if (minutes == dt.minutes)

            return 0;

        else if (minutes > dt.minutes)

            return 1;

        else

            return -1;

//        return 0;

    }

}

ANSWER

PART B:-

I followed the given methods to add mylinkedlist

import java.util.List;

public class MyLinkedList {

    static Node head;

    class Node {

        int data;

        Node next;

        Node(int d) {

            data = d;

            next = null;

        }

    }

    void sortedInsert(Node new_node) {

        Node current;

        if (head == null || head.data >= new_node.data) {

            new_node.next = head;

            head = new_node;

        } else {

            current = head;

            while (current.next != null &&

                    current.next.data < new_node.data)

                current = current.next;

            new_node.next = current.next;

            current.next = new_node;

        }

    }

    Node newNode(int data) {

        Node x = new Node(data);

        return x;

    }

    void printList(Node head) {

        Node temp = MyLinkedList.head;

        while (temp != null) {

            System.out.print(temp.data + " ");

            temp = temp.next;

        }

    }

    void deleteNode(int position) {

        if (head == null)

            return;

        Node temp = head;

        if (position == 0) {

            head = temp.next;

            return;

        }

        for (int i = 0; temp != null && i < position - 1; i++)

            temp = temp.next;

        if (temp == null || temp.next == null)

            return;

        Node next = temp.next.next;

        temp.next = next;

    }

    public static Integer findMin(List<Integer> list) {

        return list.stream()

                .mapToInt(v -> v)

                .min()

                .orElse(Integer.MAX_VALUE);

    }

    Node reverse(Node node) {

        Node prev = null;

        Node current = node;

        Node next = null;

        while (current != null) {

            next = current.next;

            current.next = prev;

            prev = current;

            current = next;

        }

        node = prev;

        return node;

    }

    public static void main(String args[]) {

        MyLinkedList llist = new MyLinkedList();

        Node new_node;

        new_node = llist.newNode(5);

        llist.sortedInsert(new_node);

        new_node = llist.newNode(10);

        llist.sortedInsert(new_node);

        new_node = llist.newNode(7);

        llist.sortedInsert(new_node);

        new_node = llist.newNode(3);

        llist.sortedInsert(new_node);

        new_node = llist.newNode(1);

        llist.sortedInsert(new_node);

        new_node = llist.newNode(9);

        llist.sortedInsert(new_node);

        System.out.println("Sorted List in ascending order");

        llist.printList(head);

        System.out.println("Delete the list node at 1st position");

        llist.deleteNode(0);

        llist.printList(head);

        System.out.println("Delete the list node at the last position");

        llist.deleteNode(5);

        llist.printList(head);

        System.out.println("Minimum element is " + findMin((List<Integer>) llist));

        llist.printList(head);

        head = llist.reverse(head);

        System.out.println("Reversed linked list ");

        llist.printList(head);

    }

}

ANSWER:-

PART C:-

As per given instruction I followed

import java.util.Iterator;

import java.util.LinkedList;

import java.util.List;

class Test3 {

    static MyLinkedList.Node head;

    class Node {

        int data;

        MyLinkedList.Node next;

        Node(int d) {

            data = d;

            next = null;

        }

    }

    void sortedInsert(MyLinkedList.Node new_node) {

        MyLinkedList.Node current;

        if (head == null || head.data >= new_node.data) {

            new_node.next = head;

            head = new_node;

        } else {

            current = head;

            while (current.next != null &&

                    current.next.data < new_node.data)

                current = current.next;

            new_node.next = current.next;

            current.next = new_node;

        }

    }

    MyLinkedList.Node newNode(int data) {

        MyLinkedList.Node x = new MyLinkedList.Node(data);

        return x;

    }

    void printList(MyLinkedList.Node head) {

        MyLinkedList.Node temp = MyLinkedList.head;

        while (temp != null) {

            System.out.print(temp.data + " ");

            temp = temp.next;

        }

    }

    void deleteNode(int position) {

        if (head == null)

            return;

        MyLinkedList.Node temp = head;

        if (position == 0) {

            head = temp.next;

            return;

        }

        for (int i = 0; temp != null && i < position - 1; i++)

            temp = temp.next;

        if (temp == null || temp.next == null)

            return;

        MyLinkedList.Node next = temp.next.next;

        temp.next = next;

    }

    public static Integer findMin(List<Integer> list) {

        return list.stream()

                .mapToInt(v -> v)

                .min()

                .orElse(Integer.MAX_VALUE);

    }

    MyLinkedList.Node reverse(MyLinkedList.Node node) {

        MyLinkedList.Node prev = null;

        MyLinkedList.Node current = node;

        MyLinkedList.Node next = null;

        while (current != null) {

            next = current.next;

            current.next = prev;

            prev = current;

            current = next;

        }

        node = prev;

        return node;

    }

    public static void main(String[] args) {

        LinkedList dateTimeList = new LinkedList<>();

        dateTimeList.add(19);

        dateTimeList.add(5);

        dateTimeList.add("today");

        dateTimeList.add(20);

        dateTimeList.add(60);

        System.out.println(dateTimeList);

        int ind = dateTimeList.indexOf("today");

        for (int i = ind; i >= ind; i++) {

            Iterator<String> itr = dateTimeList.iterator();

            while (itr.hasNext()) {

                System.out.println(itr.next());

            }

            dateTimeList.add(40);

            System.out.println(dateTimeList);

            MyLinkedList llist = new MyLinkedList();

            MyLinkedList.Node new_node;

            new_node = llist.newNode(5);

            llist.sortedInsert(new_node);

            System.out.println("Sorted List in ascending order");

            llist.printList(head);

            head = llist.reverse(head);

            System.out.println("Reversed linked list ");

            llist.printList(head);

            System.out.println("Smallest element in the list");

            llist.printList(0);

            System.out.println("Last Largest in the list");

            llist.printList(5);

        }

dateTimeList.add("afternoon");

Iterator<String> itr1 = dateTimeList.iterator();

while (itr1.hasNext()) {

System.out.println(itr1.next());

}

System.out.print("Delete the first element from the list");

llist.deleteNode(0);

llist.printList(head);

System.out.print("Delete the last element from the list");

llist.deleteNode(6);

llist.printList(head);

dateTimeList2.add(28);

dateTimeList2.add(55);

dateTimeList2.addAll(dateTimeList);

Iterator<String> itr2 = dateTimeList2.iterator();

while (itr2.hasNext()) {

System.out.println(itr2.next());

}

    }

}