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; } } }
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;
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images