鱼C论坛

 找回密码
 立即注册
查看: 1200|回复: 2

JAVA

[复制链接]
发表于 2023-11-12 17:35:04 | 显示全部楼层
当使用Java编程实现循环链表的泛型类时,可以按照以下方式设计:

[/code]java
public class CircularLinkedList<T> {
    private Node<T> head;
    private int size;

    private static class Node<T> {
        T data;
        Node<T> next;

        Node(T data) {
            this.data = data;
        }
    }

    public CircularLinkedList() {
        head = null;
        size = 0;
    }

    public int size() {
        return size;
    }

    public void insert(T data) {
        Node<T> newNode = new Node<>(data);
        if (head == null) {
            head = newNode;
            head.next = head;  // 构成循环
        } else {
            Node<T> current = head;
            while (current.next != head) {
                current = current.next;
            }
            current.next = newNode;  // 添加到末尾
            newNode.next = head;  // 新结点指向头结点
        }
        size++;
    }

    public void delete(T data) {
        if (head == null) {
            return;
        }
        if (head.data.equals(data)) {
            if (head.next == head) {
                head = null;  // 只有一个结点时直接删除
            } else {
                Node<T> current = head;
                while (current.next != head) {
                    current = current.next;
                }
                head = head.next;  // 移动头指针
                current.next = head;  // 尾结点指向新头结点
            }
            size--;
            return;
        }
        Node<T> current = head;
        Node<T> prev = null;
        do {
            prev = current;
            current = current.next;
            if (current.data.equals(data)) {
                prev.next = current.next;  // 跳过当前结点
                size--;
                return;
            }
        } while (current != head);
    }

    public boolean contains(T data) {
        if (head == null) {
            return false;
        }
        Node<T> current = head;
        do {
            if (current.data.equals(data)) {
                return true;
            }
            current = current.next;
        } while (current != head);
        return false;
    }

    public void set(int index, T data) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index out of bounds");
        }
        Node<T> current = head;
        for (int i = 0; i < index; i++) {
            current = current.next;
        }
        current.data = data;
    }

    public void traverse() {
        if (head == null) {
            return;
        }
        Node<T> current = head;
        do {
            System.out.print(current.data + " ");
            current = current.next;
        } while (current != head);
    }
}
[/code]

在上述代码中,我们定义了一个泛型类 CircularLinkedList,其中包含了创建循环链表、插入新的结点、删除指定元素结点、查找指定元素结点、修改指定位置结点、以及遍历循环链表的基本功能。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-26 04:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表