ZBLOG

void addAll(int i, DoublyList list)用java写,其中包括class DoublyList,DoublyList(),boolean isEmpty(),String toString(),DoublyList(T[] values),DoublyList(DoublyList list),void concat(int i, DoublyList list),void addAll(int i, DoubleList lis...

下面是根据您提供的要求编写的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助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=8559

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?