Rewrite this code so it can use a generic type instead of integers. This means we can use any data type with it.  For example: LinkedList = new LinkedLIst<>(); public class Main {     public static void main(String[] args) {         /*          * Expected output:          *          * The list: 8 -> 1 -> 5 -> 3 -> 6          * Size: 5          * Head: 8          * Tail: 6          * Removeing 1...          * The list: 8 -> 5 -> 3 -> 6          * Size: 4          * Head: 8          * Tail: 6          * Removeing head...          * The list: 5 -> 3 -> 6          * Size: 3          * Head: 5          * Tail: 6          * Removeing tail...          * The list: 5 -> 3          * Size: 2          * Head: 5          * Tail: 3          */                  LinkedList list = new LinkedList();         list.append(5);         list.append(3);         list.append(6);         list.prepend(1);         list.prepend(8);         printListDetails(list);         System.out.println("Removeing 1...");         list.removeByValue(1);         printListDetails(list);         System.out.println("Removeing head...");         list.removeHead();         printListDetails(list);         System.out.println("Removeing tail...");         list.removeTail();         printListDetails(list);     }     public static void printListDetails(LinkedList list) {         System.out.println("The list: " + list.toString());         System.out.println("Size: " + list.size());         System.out.println("Head: " + list.getHead());         System.out.println("Tail: " + list.getTail());     } } ________________________________________ public class LinkedList {     private Node head;     private Node tail;     private int size;     public LinkedList() {         head = null;         tail = null;         size = 0;     }     public void append(int data) {         Node newNode = new Node(data);         if (head == null) {             head = newNode;         } else {             tail.next = newNode;         }         tail = newNode;         size++;     }     public void prepend(int data) {         Node newNode = new Node(data);         if (head == null) {             head = newNode;             tail = newNode;         } else {             newNode.next = head;             head = newNode;         }         size++;     }     public int getHead() {         return head.data;     }     public int getTail() {         return tail.data;     }     public int size() {         return size;     }     public void removeByValue(int data) {         Node current = head;         while (current.next != null && current.next.data != data) {             current = current.next;         }         if (current.next != null) {             current.next = current.next.next;             size--;             if (current.next == null) {                 tail = current;             }         }     }     public int removeHead() {         int removedData = head.data;         head = head.next;         size--;         if (head == null) {             tail = null;         }         return removedData;     }     public int removeTail() {         int removedData = tail.data;         if (head == tail) {             head = null;             tail = null;         } else {             Node current = head;             while (current.next != tail) {                 current = current.next;             }             tail = current;             tail.next = null;         }         size--;         return removedData;     }     @Override     public String toString() {         StringBuilder builder = new StringBuilder();         Node current = head;         while (current != null) {             builder.append(current.data);             if (current.next != null) {                 builder.append(" -> ");             }             current = current.next;         }         return builder.toString();     }     private class Node {         public Node next;         public int data;         public Node(int data) {             this.data = data;         }     } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Rewrite this code so it can use a generic type instead of integers. This means we can use any data type with it. 

For example:
LinkedList<String> = new LinkedLIst<>();

public class Main {
    public static void main(String[] args) {
        /*
         * Expected output:
         *
         * The list: 8 -> 1 -> 5 -> 3 -> 6
         * Size: 5
         * Head: 8
         * Tail: 6
         * Removeing 1...
         * The list: 8 -> 5 -> 3 -> 6
         * Size: 4
         * Head: 8
         * Tail: 6
         * Removeing head...
         * The list: 5 -> 3 -> 6
         * Size: 3
         * Head: 5
         * Tail: 6
         * Removeing tail...
         * The list: 5 -> 3
         * Size: 2
         * Head: 5
         * Tail: 3
         */
        
        LinkedList list = new LinkedList();

        list.append(5);
        list.append(3);
        list.append(6);

        list.prepend(1);
        list.prepend(8);

        printListDetails(list);

        System.out.println("Removeing 1...");
        list.removeByValue(1);
        printListDetails(list);

        System.out.println("Removeing head...");
        list.removeHead();
        printListDetails(list);

        System.out.println("Removeing tail...");
        list.removeTail();
        printListDetails(list);
    }

    public static void printListDetails(LinkedList list) {
        System.out.println("The list: " + list.toString());
        System.out.println("Size: " + list.size());
        System.out.println("Head: " + list.getHead());
        System.out.println("Tail: " + list.getTail());
    }
}


________________________________________

public class LinkedList {
    private Node head;
    private Node tail;
    private int size;

    public LinkedList() {
        head = null;
        tail = null;
        size = 0;
    }

    public void append(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        } else {
            tail.next = newNode;
        }
        tail = newNode;
        size++;
    }

    public void prepend(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            tail = newNode;
        } else {
            newNode.next = head;
            head = newNode;
        }
        size++;
    }

    public int getHead() {
        return head.data;
    }

    public int getTail() {
        return tail.data;
    }

    public int size() {
        return size;
    }

    public void removeByValue(int data) {
        Node current = head;
        while (current.next != null && current.next.data != data) {
            current = current.next;
        }
        if (current.next != null) {
            current.next = current.next.next;
            size--;
            if (current.next == null) {
                tail = current;
            }
        }
    }

    public int removeHead() {
        int removedData = head.data;
        head = head.next;
        size--;
        if (head == null) {
            tail = null;
        }
        return removedData;
    }

    public int removeTail() {
        int removedData = tail.data;
        if (head == tail) {
            head = null;
            tail = null;
        } else {
            Node current = head;
            while (current.next != tail) {
                current = current.next;
            }
            tail = current;
            tail.next = null;
        }
        size--;
        return removedData;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        Node current = head;
        while (current != null) {
            builder.append(current.data);
            if (current.next != null) {
                builder.append(" -> ");
            }
            current = current.next;
        }
        return builder.toString();
    }

    private class Node {
        public Node next;
        public int data;

        public Node(int data) {
            this.data = data;
        }
    }
}

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Concept of Threads
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education