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);
    }
    
    @Override
    public String toString() {
        if (isEmpty()) {
            return "Empty List";
        }
        
        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) {
          addLast(value);
      }
  }

  public DoublyList(DoublyList<T> list) {
      if (!list.isEmpty()) {
          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();
      }

      if (!list.isEmpty()) {

          if (i == size()) { // Concatenate at the end
              Node<T> current = list.head;

              while (current != null) {
                  addLast(current.data);
                  current = current.next;
              }
          } else { // Concatenate in the middle

              Node<T> currentNode = getNode(i); 
              Node<T> nextNode = currentNode.next; 

              currentNode.next = list.head; 
              list.head.prev = currentNode; 

              Node<T> lastNodeOfListToConcatenate = list.tail;

              lastNodeOfListToConcatenate.next = nextNode; 
              if (nextNode != null) {
                  nextNode.prev = lastNodeOfListToConcatenate;
              } else { // Concatenating at the end
                  tail = lastNodeOfListToConcatenate;
              }

          }
      }
  }

  public void addAll(int i, DoublyList<T> list) {
      if (i < 0 || i > size()) {
          throw new IndexOutOfBoundsException();
      }

      if (!list.isEmpty()) {

          Node<T> current = list.head;

          while (current != null) {
              insert(i++, current.data);
              current = current.next;
          }
      }
  }

    // Helper methods for adding and inserting nodes
    private void addLast(T data) {
        Node<T> newNode = new Node<>(data);

        if (isEmpty()) {
            head = newNode;
            tail = newNode;
        } else {
            tail.next = newNode;
            newNode.prev = tail;
            tail = newNode;
        }
    }

    private void insert(int index, T data) {
        if (index == 0) { // Insert at the beginning
            Node<T> newNode = new Node<>(data);
            newNode.next = head;
            
            if (head != null) {
                head.prev = newNode;
            } else { // Empty List
                tail = newNode; // Update tail reference
            }
            
            head = newNode; // Update head reference
            
        } else { // Insert in the middle or at the end
            
            Node<T> prevNode = getNode(index - 1); 
            
            if (prevNode == null || prevNode == tail) { // Insert at the end
                addLast(data);
                
            } else { // Insert in the middle
                
                Node<T> nextNode = prevNode.next; 
                
                Node<T> newNode = new Node<>(data);
                newNode.prev = prevNode;
                newNode.next = nextNode;
                
                prevNode.next = newNode;
                nextNode.prev = newNode;
            }
        }
    }

    // Helper method for getting node at a specific index
    private Node<T> getNode(int index) {
        if (index < 0 || index >= size()) {
            throw new IndexOutOfBoundsException();
        }

        if (index <= size() / 2) { // Search from the beginning
            Node<T> current = head;

            for (int i = 0; i < index; i++) {
                current = current.next;
            }

            return current;

        } else { // Search from the end
            Node<T> current = tail;

            for (int i = size() - 1; i > index; i--) {
                current = current.prev;
            }

            return current;
        }
    }

    // Helper method to calculate the size of the list
    private int size() {
        int count = 0;
        
        Node<T> current = head;

        while (current != null) {
            count++;
            current = current.next;
        }

        return count;
    }

    // Definition of a doubly linked list node
    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 class Main {
   public static void main(String[] args) {

       DoublyList<Integer> myList1 = new DoublyList<>();
       myList1.addLast(1);
       myList1.addLast(2);
       myList1.addLast(3);

       DoublyList<Integer> myList2= new DoublyList<>(new Integer[]{4, 5, 6});

       DoublyList<Integer> myList3 = new DoublyList<>(myList1);

       System.out.println("Original list 1: " + myList1.toString());
       System.out.println("Original list 2: " + myList2.toString());
       System.out.println("Original list 3: " + myList3.toString());

       // Concatenate myList2 at the end of myList1
       myList1.concat(myList1.size(), myList2);
       System.out.println("Concatenated list (list2 appended at the end): " + myList1.toString());

       // Insert myList2 in the middle of myList3 at index 1
       myList3.addAll(1, myList2);
       System.out.println("Inserted list (list2 inserted in the middle of list3): " + myList3.toString());

   }
}

以上是一个使用DoublyList类实现的示例代码,包括了类的定义、方法的实现以及在main函数中进行验证。


内容由零声教学AI助手提供,问题来源于学员提问

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?