Java实现 循环链表

循环链表和单向链表的不同之处在于 :
单向链表tail.next指向null;
而循环链表tail.next指向head;

循环链表类:

public class CLink {

    public Node head;

    public CLink() {
        this.head = new Node();
        head.next = head;
    }

    public void andNode(String name) {
        /**
         * 创建需要and的节点
         */
        Node and = new Node(name,null);
        Node p;
        if(head.next == head) { //空表
            and.next = head;
            head.next = and;
        }else {
            p = head; //首节点
            while (p.next != head) {//向下遍历
                p = p.next;
            }
            and.next = p.next;
            p.next = and;
        }
    }

    public boolean delNode(String name) {
        Node p = head;
        while (p.next != head) {
            if(p.next.name.equals(name)) {
                p.next = p.next.next;
                return true;
            }else {
                p = p.next;
            }
        }
        return false;
    }

    /**
     * data 之前插入节点
     * @param data
     * @param name
     * @return
     */
    public boolean insertNode(String data,String name) {
        Node p = head;
        Node insertNode = new Node(name, null);
        while (p.next != head) {
            if(p.next.name.equals(data)) {
                insertNode.next = p.next;
                p.next = insertNode;
                return true;
            }
            p = p.next;
        }
        return false;
    }

    public int size() {
        Node p = head;
        int size = 0;
        while (p.next != head) {
            p = p.next;
            size ++;
        }
        return size;
    }

    public void display() {
        Node p = head;
        while (p.next != head) {
            p = p.next;
            System.out.println(p.name);
        }
    }
}

节点类:

public class Node {
    public String name;
    public Node next;

    public Node(String name, Node next) {
        this.name = name;
        this.next = next;
    }

    public Node() {
        this.next = null;
    }

}

测试代码:

CLink cLink = new CLink();
        cLink.andNode("1");
        cLink.andNode("2");
        cLink.andNode("3");
        cLink.display();
        System.out.println("-------------------------");
        cLink.delNode("2");
        cLink.display();
        System.out.println("-------------------------");
        System.out.println("size : " + cLink.size());
        cLink.insertNode("3","2");
        cLink.display();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure! Here's an example of Java code to implement a doubly circular linked list: ```java public class DoublyCircularLinkedList { private Node head; private int size; private class Node { private int data; private Node previous; private Node next; public Node(int data) { this.data = data; } } // Constructor public DoublyCircularLinkedList() { head = null; size = 0; } // Check if the list is empty public boolean isEmpty() { return head == null; } // Get the size of the list public int getSize() { return size; } // Add an element to the beginning of the list public void addFirst(int data) { Node newNode = new Node(data); if (isEmpty()) { newNode.next = newNode; newNode.previous = newNode; head = newNode; } else { newNode.next = head; newNode.previous = head.previous; head.previous.next = newNode; head.previous = newNode; head = newNode; } size++; } // Add an element to the end of the list public void addLast(int data) { Node newNode = new Node(data); if (isEmpty()) { newNode.next = newNode; newNode.previous = newNode; head = newNode; } else { newNode.next = head; newNode.previous = head.previous; head.previous.next = newNode; head.previous = newNode; } size++; } // Remove the first element from the list public void removeFirst() { if (isEmpty()) { System.out.println("List is empty!"); return; } if (size == 1) { head = null; } else { head.next.previous = head.previous; head.previous.next = head.next; head = head.next; } size--; } // Remove the last element from the list public void removeLast() { if (isEmpty()) { System.out.println("List is empty!"); return; } if (size == 1) { head = null; } else { head.previous.previous.next = head; head.previous = head.previous.previous; } size--; } // Display the elements of the list public void display() { if (isEmpty()) { System.out.println("List is empty!"); return; } Node current = head; do { System.out.print(current.data + " "); current = current.next; } while (current != head); System.out.println(); } // Main method to test the implementation public static void main(String[] args) { DoublyCircularLinkedList list = new DoublyCircularLinkedList(); list.addFirst(3); list.addFirst(2); list.addFirst(1); list.addLast(4); list.addLast(5); System.out.println("Elements of the list: "); list.display(); list.removeFirst(); list.removeLast(); System.out.println("Elements after removing first and last: "); list.display(); } } ``` This code creates a DoublyCircularLinkedList class with methods to add elements to the beginning and end, remove elements from the beginning and end, check if the list is empty, get the size of the list, and display the elements of the list. The main method is used to test the implementation by adding elements and then removing the first and last elements.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值