下面是根据您提供的要求编写的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;
}
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();
}
public DoublyList(T[] values) {
for (T value : values) {
add(value);
}
}
public DoublyList(DoublyList<T> list) {
if (!list.isEmpty()) {
Node<T> current = list.head;
while (current != null) {
add(current.data);
current = current.next;
}
}
}
public void concat(int i, DoublyList<T> list) {
if (i < 0 || i > size()) {
throw new IndexOutOfBoundsException("Index out of range");
}
if (!list.isEmpty()) {
Node<T> insertPosNode = getNodeAt(i);
if (insertPosNode == head) { // Insert at the beginning
insertPosNode.prev = list.tail;
list.tail.next = insertPosNode;
head = list.head;
} else if (insertPosNode == null) { // Insert at the end
tail.next = list.head;
list.head.prev = tail;
tail = list.tail;
} else { // Insert in the middle
insertPosNode.prev.next = list.head;
list.head.prev = insertPosNode.prev;
insertPosNode.prev = list.tail;
list.tail.next = insertPosNode;
}
}
}
public void addAll(int i, DoublyList<T> list) {
if (i < 0 || i > size()) {
throw new IndexOutOfBoundsException("Index out of range");
}
if (!list.isEmpty()) {
Node<T> insertPosNode = getNodeAt(i);
if (insertPosNode == null) { // Add at the end
tail.next = list.head;
list.head.prev = tail;
tail = list.tail;
} else { // Add in the middle or at the beginning
if (insertPosNode.prev != null) {
insertPosNode.prev.next = list.head;
list.head.prev = insertPosNode.prev;
} else {
head = list.head;
}
insertPosNode.prev = list.tail;
list.tail.next = insertPosNode;
}
}
}
private void add(T data) {
Node<T> newNode = new Node<>(data);
if (isEmpty()) {
head = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
}
tail= newNode;
}
private int size() {
int count = 0;
Node<T> current = head;
while (current != null) {
count++;
current= current.next;
}
return count;
}
private Node<T> getNodeAt(int index) {
if (index >= 0 && index < size()) {
Node<T> current= head;
for (int i= 0; i < index; i++) {
current= current.next;
}
return current;
} else {
return null;
}
}
private static class Node<T> {
private T data;
private Node<T> prev;
private Node<T> next;
public Node(T data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
public static void main(String[] args) {
DoublyList<Integer> list1 = new DoublyList<>();
list1.add(1);
list1.add(2);
DoublyList<Integer> list2 = new DoublyList<>();
list2.add(3);
list2.add(4);
System.out.println("Initial List 1: " + list1.toString());
System.out.println("Initial List 2: " + list2.toString());
// Testing concat() method
System.out.println("\nTesting concat() method:");
DoublyList<Integer> concatList = new DoublyList<>(list1);
concatList.concat(list1.size(), list2); // Concatenating list2 at the end of list1
System.out.println("Concatenated List: " + concatList.toString());
// Testing addAll() method
System.out.println("\nTesting addAll() method:");
DoublyList<Integer> addAllList = new DoublyList<>(list1);
addAllList.addAll(0, list2); // Adding all elements of list2 at the beginning of list1
System.out.println("Updated List: " + addAllList.toString());
}
}
希望对您有所帮助!
内容由零声教学AI助手提供,问题来源于学员提问




