鱼C论坛

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

JAVA

[复制链接]
发表于 2023-11-12 17:34:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
用java编程完成泛型类CircularLinkedList(循环链表),CircularLinkedList的基本功能设计要求:创建循环链表、插入新的结点、删除指定元素结点、查找指定元素结点、修改指定位置结点、遍历循环链表。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-12 17:34:36 | 显示全部楼层
import java.util.Iterator;

public class CircularLinkedList<E> implements Iterable<E> {
    private Node<E> head;
    private int size;

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

        public Node(E data) {
            this.data = data;
        }
    }

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

    public void insert(E data) {
        Node<E> newNode = new Node<>(data);
        if (head == null) {
            head = newNode;
            head.next = head;
        } else {
            Node<E> temp = head;
            while (temp.next != head) {
                temp = temp.next;
            }
            temp.next = newNode;
            newNode.next = head;
        }
        size++;
    }

    public void delete(E data) {
        if (head != null) {
            if (head.data.equals(data)) {
                if (head.next == head) {
                    head = null;
                } else {
                    Node<E> temp = head;
                    while (temp.next != head) {
                        temp = temp.next;
                    }
                    temp.next = head.next;
                    head = head.next;
                }
                size--;
            } else {
                Node<E> current = head;
                Node<E> previous = null;
                do {
                    if (current.data.equals(data)) {
                        previous.next = current.next;
                        size--;
                        break;
                    }
                    previous = current;
                    current = current.next;
                } while (current != head);
            }
        }
    }

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

    public void modify(int index, E data) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException();
        }
        Node<E> temp = head;
        for (int i = 0; i < index; i++) {
            temp = temp.next;
        }
        temp.data = data;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            private Node<E> current = head;
            private int count = 0;

            @Override
            public boolean hasNext() {
                return count < size;
            }

            @Override
            public E next() {
                E data = current.data;
                current = current.next;
                count++;
                return data;
            }
        };
    }
}

这是一个基本的泛型循环链表类的实现。它包括了创建循环链表、插入新的结点、删除指定元素结点、查找指定元素结点、修改指定位置结点、遍历循环链表的功能。在这个实现中,我们使用了内部类Node来表示链表中的节点,并实现了Iterator接口来实现遍历功能。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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,其中包含了创建循环链表、插入新的结点、删除指定元素结点、查找指定元素结点、修改指定位置结点、以及遍历循环链表的基本功能。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-14 20:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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