ZBLOG

CirDoublyList(CirDoublyList list)用java语言写,完成循环双链表的深拷贝,其中使用for循环,insert方法,CirDoublyList(),String toString(),boolean isEmpty(),DoubleNode insert(int i,T x),DoubleNode insert(T x),不使用append方法,加上main函数验证

下面是使用Java语言编写的CirDoublyList类,实现循环双链表的深拷贝:

class DoubleNode<T> {
    T data;
    DoubleNode<T> prev, next;

    public DoubleNode(T data) {
        this.data = data;
        this.prev = null;
        this.next = null;
    }
}

class CirDoublyList<T> {
    private DoubleNode<T> head;

    public CirDoublyList() {
        head = new DoubleNode<>(null);
        head.prev = head;
        head.next = head;
    }

    public CirDoublyList(CirDoublyList<T> list) {
        if (list.isEmpty()) {
            head = new DoubleNode<>(null);
            head.prev = head;
            head.next = head;
            return;
        }

        // 创建新的头结点
        this.head = new DoubleNode<>(null);
        this.head.prev = this.head;

        // 复制第一个节点,并设置为头结点的后继节点
        DoubleNode<T> newNode = new DoubleNode<>(list.head.next.data);
        newNode.prev = this.head;
        newNode.next = newNode;  // 循环链表只有一个节点时,指向自身
        this.head.next = newNode;

        // 指针用于遍历原始链表和新链表
        DoubleNode<T> p1, p2;

        // 使用for循环复制剩余的节点
        for (p1 = list.head.next.next, p2=this.head.next; p1 != list.head; p1=p1.next, p2=p2.next) {
            newNode = new DoubleNode<>(p1.data);  // 复制节点
            newNode.prev = p2;
            newNode.next = this.head;  // 设置为循环链表的尾结点

            // 将新节点插入到新链表中
            p2.next = newNode;
        }
    }

    public boolean isEmpty() {
        return head.next == head;
    }

    public DoubleNode<T> insert(int i, T x) {
        if (x == null)
            throw new NullPointerException("x == null");

        DoubleNode<T> front = head;

        // 寻找插入位置的前驱节点
        for (int j = 0; front.next != head && j < i; j++)
            front = front.next;

        // 创建新节点并插入链表
        DoubleNode<T> newNode = new DoubleNode<>(x);
        newNode.prev = front;
        newNode.next = front.next;
        front.next.prev = newNode;
        front.next = newNode;

        return newNode;
    }

    public DoubleNode<T> insert(T x) {
        if (x == null)
            throw new NullPointerException("x == null");

        // 创建新节点并插入链表末尾
        DoubleNode<T> newNode = new DoubleNode<>(x);
        
         if(isEmpty()){
           this.head=  new  Node(x,null,null);
           head.setNext(head);
          }else{
              node=head.getNext();
              while(node.getNext()!=head){
                 node=node.getNext();         
              }
              node.setNext(new  Node(x,node,head));
          } 

       return newNode;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        
       if(isEmpty()) {
             sb.append("Empty list");
             return sb.toString();
         } 
        
      node=head.getNext();
      while(node!=head){
           sb.append(node.getData().toString());
           node=node.getNext();
      }

        return sb.toString();
    }
}

public class Main {
    public static void main(String[] args) {
        CirDoublyList<Integer> list1 = new CirDoublyList<>();
        list1.insert(1);
        list1.insert(2);
        list1.insert(3);

        // 测试深拷贝
        CirDoublyList<Integer> list2 = new CirDoublyList<>(list1);

        System.out.println("原始链表:");
        System.out.println(list1.toString());

        System.out.println("深拷贝的链表:");
        System.out.println(list2.toString());
    }
}

运行以上代码,可以验证循环双链表的深拷贝是否正确。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?