ZBLOG

public class CirDoublyList { public DoubleNode head; public DoubleNode rear; public CirDoublyList() { this.head=new DoubleNode(); this.head.prev=this.head; this.head.next=this.head; } publi...

public class CirDoublyList {

public DoubleNode<T> head;
public DoubleNode<T> rear;

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

public String toString() {
    StringBuilder str = new StringBuilder(this.getClass().getName() + "(");
    for (DoubleNode<T> p = this.head.next; p != this.head; p = p.next)
        str.append(p.data.toString()).append(p.next != this.head ? "," : "");
    return str + ")";
}

public DoubleNode<T> insert(T x) {
    if (x == null)
        return null;
    DoubleNode<T> q = new DoubleNode<>(x, head.prev, head);
    head.prev.next = q;
    head.prev = q;
    return q;
}

public void concat(CirDoublyList<T> list) {
    if (list == null || list.isEmpty())
        return;

     DoubleNode<T> rear1 = this.rear.prev;
     DoubleNode<T> front2= list.head.next;

     rear1.next=front2;
     front2.prev=rear1;

     list.rear.prev=this.rear;
     this.rear=list.rear;

     // 清空list
     list.clear();
}

 // 在循环双链表后面添加一个双链表
 public void addAll(DoublyList<T> list) {
      if (list == null || list.isEmpty())
          return;

      for (DoubleNode<T> p = list.head.next; p != null; p=p.next){
            insert(p.data);
      }
 }

 // 返回一个新的循环双链表,包含当前循环双链表和给定双链表的所有元素
 public CirDoublyList<T> union(DoublyList<T> list) {
      CirDoublyList<T> result = new CirDoublyList<>(this);
      result.addAll(list);
      return result;
 }

 // 判断循环双链表是否为空
 public boolean isEmpty() {
     return this.head.next == this.head;
 }

 // 清空循环双链表
 public void clear() {
     this.head.prev = this.head;
     this.head.next = this.head;
 }

public static void main(String[] args) {
    CirDoublyList<Integer> list1 = new CirDoublyList<>();
    DoublyList<Integer> list2 = new DoublyList<>();
    for (int i = 1; i <= 5; i++)
        list1.insert(i);
    for (int i = 6; i <= 10; i++)
        list2.insert(i);
    System.out.println(list1.toString());
    System.out.println(list2.toString());
    list1.concat(new CirDoublyList<>(list2));
    System.out.println(list1.toString());
}

}


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?