📅  最后修改于: 2023-12-03 14:58:28.340000             🧑  作者: Mango
Design a program to implement a generic LinkedList class in Java. Your LinkedList class should have the following methods:
public void insertFront(T data)
: Inserts an element at the front of the list.public void insertBack(T data)
: Inserts an element at the back of the list.public void deleteFront()
: Deletes the element at the front of the list.public void deleteBack()
: Deletes the element at the back of the listpublic void printList()
: Prints the elements of the list.Here is the implementation of the LinkedList class in Java:
public class LinkedList<T> {
private Node<T> head;
private Node<T> tail;
private class Node<T> {
private T data;
private Node<T> next;
public Node(T data) {
this.data = data;
this.next = null;
}
}
public void insertFront(T data) {
Node<T> newNode = new Node<>(data);
if (head == null) {
head = newNode;
tail = newNode;
} else {
newNode.next = head;
head = newNode;
}
}
public void insertBack(T data) {
Node<T> newNode = new Node<>(data);
if (tail == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
}
public void deleteFront() {
if (head == null) {
return;
}
if (head == tail) {
head = null;
tail = null;
} else {
head = head.next;
}
}
public void deleteBack() {
if (tail == null) {
return;
}
if (head == tail) {
head = null;
tail = null;
} else {
Node<T> curr = head;
while (curr.next != tail) {
curr = curr.next;
}
curr.next = null;
tail = curr;
}
}
public void printList() {
Node<T> curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
}
The LinkedList class uses a private inner class Node
to construct a node containing the data and a reference to the next node in the list. The LinkedList class has instance variables head
and tail
which keep track of the first and last nodes in the list, respectively.
The insertFront
method inserts a new node at the front of the list. If the list is empty, head
and tail
both point to the new node. Otherwise, the new node becomes the new head
node and its next
reference points to the previous head
node.
The insertBack
method inserts a new node at the end of the list. If the list is empty, head
and tail
both point to the new node. Otherwise, the previous tail
node's next
reference points to the new node and the new node becomes the new tail
node.
The deleteFront
method removes the first node in the list. If the list is empty, nothing happens. If the list has only one node, head
and tail
are set to null
, indicating an empty list. Otherwise, head
is set to the next node in the list.
The deleteBack
method removes the last node in the list. If the list is empty, nothing happens. If the list has only one node, head
and tail
are set to null
. Otherwise, it traverses the list to find the second-to-last node, sets its next
reference to null
and sets that node as the new tail
.
The printList
method traverses the list and prints out each element followed by a space. It then prints a new line.
The LinkedList class is a fundamental data structure in computer science. This implementation provides a basic, generic implementation of a singly linked list in Java. It can be used in many different applications, including big data processing, machine learning, and web development.