鱼C论坛

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

Java循环链表

[复制链接]
发表于 2023-11-13 10:19:51 | 显示全部楼层 |阅读模式

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

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

x
java请编程完成泛型类CircularLinkedList(循环链表),CircularLinkedList的基本功能设计要求:创建循环链表、插入新的结点、删除指定元素结点、查找指定元素结点、修改指定位置结点、遍历循环链表。并写测试类
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-13 10:20:07 | 显示全部楼层
  1. import java.util.Iterator;

  2. public class CircularLinkedList<E> implements Iterable<E> {
  3.     private Node<E> head;
  4.     private int size;

  5.     private static class Node<E> {
  6.         E data;
  7.         Node<E> next;

  8.         Node(E data) {
  9.             this.data = data;
  10.         }
  11.     }

  12.     public void insert(E data) {
  13.         if (head == null) {
  14.             head = new Node<>(data);
  15.             head.next = head;
  16.         } else {
  17.             Node<E> newNode = new Node<>(data);
  18.             newNode.next = head.next;
  19.             head.next = newNode;
  20.         }
  21.         size++;
  22.     }

  23.     public void delete(E data) {
  24.         if (head == null) {
  25.             return;
  26.         }

  27.         Node<E> current = head;
  28.         Node<E> previous = null;

  29.         do {
  30.             if (current.data.equals(data)) {
  31.                 if (previous != null) {
  32.                     previous.next = current.next;
  33.                 } else {
  34.                     head = current.next;
  35.                 }
  36.                 size--;
  37.                 return;
  38.             }
  39.             previous = current;
  40.             current = current.next;
  41.         } while (current != head);
  42.     }

  43.     public Node<E> find(E data) {
  44.         Node<E> current = head;
  45.         do {
  46.             if (current.data.equals(data)) {
  47.                 return current;
  48.             }
  49.             current = current.next;
  50.         } while (current != head);
  51.         return null;
  52.     }

  53.     public void modify(int index, E newData) {
  54.         if (index < 0 || index >= size) {
  55.             throw new IndexOutOfBoundsException("Index out of bounds");
  56.         }

  57.         Node<E> current = head;
  58.         for (int i = 0; i < index; i++) {
  59.             current = current.next;
  60.         }
  61.         current.data = newData;
  62.     }

  63.     @Override
  64.     public Iterator<E> iterator() {
  65.         return new Iterator<E>() {
  66.             private Node<E> current = head;
  67.             private int count = 0;

  68.             @Override
  69.             public boolean hasNext() {
  70.                 return count < size;
  71.             }

  72.             @Override
  73.             public E next() {
  74.                 E data = current.data;
  75.                 current = current.next;
  76.                 count++;
  77.                 return data;
  78.             }
  79.         };
  80.     }
  81. }

  82. // 测试类
  83. public class CircularLinkedListTest {
  84.     public static void main(String[] args) {
  85.         CircularLinkedList<Integer> list = new CircularLinkedList<>();
  86.         list.insert(1);
  87.         list.insert(2);
  88.         list.insert(3);
  89.         
  90.         for (Integer data : list) {
  91.             System.out.println(data);
  92.         }
  93.         
  94.         list.delete(2);
  95.         
  96.         for (Integer data : list) {
  97.             System.out.println(data);
  98.         }
  99.         
  100.         list.modify(1, 5);
  101.         
  102.         for (Integer data : list) {
  103.             System.out.println(data);
  104.         }
  105.     }
  106. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-13 10:21:01 | 显示全部楼层
[/code]java
public class CircularLinkedList<T> {
    private Node<T> head;
    private int size;

    // 构造函数
    public CircularLinkedList() {
        head = null;
        size = 0;
    }

    // 结点类
    private static class Node<T> {
        T data;
        Node<T> next;

        Node(T data, Node<T> next) {
            this.data = data;
            this.next = next;
        }
    }

    // 插入新的结点
    public void insert(T data) {
        if (head == null) {
            head = new Node<>(data, null);
            head.next = head;  // 单结点成环
        } else {
            Node<T> newNode = new Node<>(data, head.next);
            head.next = newNode;
        }
        size++;
    }

    // 删除指定元素结点
    public void delete(T data) {
        if (head != null) {
            Node<T> current = head;
            while (current.next != head && !current.next.data.equals(data)) {
                current = current.next;
            }
            if (current.next.data.equals(data)) {
                current.next = current.next.next;
                size--;
            }
        }
    }

    // 查找指定元素结点
    public boolean search(T data) {
        Node<T> current = head;
        do {
            if (current.data.equals(data)) {
                return true;
            }
            current = current.next;
        } while (current != head);
        return false;
    }

    // 修改指定位置结点
    public void modify(int position, T newData) {
        if (position < 0 || position >= size) {
            throw new IndexOutOfBoundsException("Invalid position");
        }

        Node<T> current = head;
        for (int i = 0; i < position; i++) {
            current = current.next;
        }

        current.data = newData;
    }

    // 遍历循环链表
    public void traverse() {
        Node<T> current = head;
        do {
            System.out.print(current.data + " ");
            current = current.next;
        } while (current != head);
        System.out.println();
    }

    // 测试类
    public static void main(String[] args) {
        CircularLinkedList<Integer> list = new CircularLinkedList<>();
        list.insert(1);
        list.insert(2);
        list.insert(3);

        list.traverse(); // 输出:3 2 1

        list.delete(2);
        list.traverse(); // 输出:3 1

        System.out.println(list.search(3));  // 输出:true

        list.modify(1, 4);
        list.traverse(); // 输出:3 4
    }
}
[/code]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-3 12:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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