以下是根据您提供的要求给出的Java代码实现:
public class DoublyList<T> {
private Node<T> head;
private Node<T> tail;
public DoublyList() {
head = null;
tail = null;
}
public boolean isEmpty() {
return head == null;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Node<T> current = head;
while (current != null) {
sb.append(current.data).append(" ");
current = current.next;
}
return sb.toString().trim();
}
public DoublyList(T[] values) {
for (T value : values) {
addLast(value);
}
}
public DoublyList(DoublyList<T> list) {
Node<T> current = list.head;
while (current != null) {
addLast(current.data);
current = current.next;
}
}
public void concat(int i, DoublyList<T> list) {
if (i < 0 || i > size()) {
throw new IndexOutOfBoundsException("Invalid index: " + i);
}
if (isEmpty()) { // If the list is empty, set head and tail to the given list's head and tail
this.head = list.head;
this.tail = list.tail;
return;
}
if (i == 0) { // Concatenate at the beginning
list.tail.next = this.head;
this.head.prev = list.tail;
this.head = list.head;
return;
}
Node<T> nodeAtIndexIminus1 = getNode(i - 1); // Get the node at index (i-1)
if (nodeAtIndexIminus1 == null) {
throw new IndexOutOfBoundsException("Invalid index: " + i);
}
Node<T> nodeAtIndexI = nodeAtIndexIminus1.next; // Get the node at index i
if (nodeAtIndexI == null) {
throw new IndexOutOfBoundsException("Invalid index: " + i);
}
list.tail.next = nodeAtIndexI;
nodeAtIndexI.prev = list.tail;
nodeAtIndexIminus1.next = list.head;
list.head.prev = nodeAtIndexIminus1;
}
public void addAll(int i, DoublyList<T> list) {
if (i < 0 || i > size()) {
throw new IndexOutOfBoundsException("Invalid index: " + i);
}
if (isEmpty()) { // If the list is empty, set head and tail to the given list's head and tail
this.head = list.head;
this.tail = list.tail;
return;
}
Node<T> nodeAtIndexIminus1 = getNode(i - 1); // Get the node at index (i-1)
if (nodeAtIndexIminus1 == null) {
throw new IndexOutOfBoundsException("Invalid index: " + i);
}
Node<T> nodeAtIndexI = nodeAtIndexIminus1.next; // Get the node at index i
if (nodeAtIndexI == null) {
throw new IndexOutOfBoundsException("Invalid index: " + i);
}
Node<T> firstNodeOfListToAdd = list.head;
firstNodeOfListToAdd.prev = nodeAtIndexIminus1;
lastNodeOfListToAdd.next= nodeAtIndex I
nodeAtindeximinusl .nextfirtNoeO fLtA
}
private int size() {
int count = 0;
Node<T> current = head;
while (current != null) {
count++;
current = current.next;
}
return count;
}
private Node<T> getNode(int index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("Invalid index: " + index);
}
Node<T> current = head;
for (int i = 0; i < index; i++) {
current = current.next;
}
return current;
}
private void addLast(T data) {
Node<T> newNode = new Node<>(data);
if (isEmpty()) {
head = newNode;
tail = newNode;
} else {
newNode.prev = tail;
tail.next = newNode;
tail = newNode;
}
}
private static class Node<T> {
private T data;
private Node<T> prev, next;
public Node(T data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
}
public class Main {
public static void main(String[] args) {
DoublyList<Integer> list1=new DoublyList<>();
list1.addLast(1);
list1.addLast(2);
DoublyList<Integer> list2=new DoublyList<>();
list2.addLast(3);
list2.addLast(4);
System.out.println("Before concatenation:");
System.out.println("list1: "+list1.toString());
System.out.println("list2: "+list2.toString());
list1.concat(1, list2);
System.out.println("\nAfter concatenation at index 1:");
System.out.println("list1: "+list1.toString());
}
}
上述代码实现了DoublyList类,包括构造函数、isEmpty方法、toString方法、concat方法和addAll方法。在Main类的main方法中,进行了一个简单的验证。运行该程序将输出拼接后的链表结果。
内容由零声教学AI助手提供,问题来源于学员提问




