鱼C论坛

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

JAVA

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

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

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

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

使用道具 举报

发表于 2023-11-12 17:34:36 | 显示全部楼层
  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.         private E data;
  7.         private Node<E> next;

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

  12.     public CircularLinkedList() {
  13.         head = null;
  14.         size = 0;
  15.     }

  16.     public void insert(E data) {
  17.         Node<E> newNode = new Node<>(data);
  18.         if (head == null) {
  19.             head = newNode;
  20.             head.next = head;
  21.         } else {
  22.             Node<E> temp = head;
  23.             while (temp.next != head) {
  24.                 temp = temp.next;
  25.             }
  26.             temp.next = newNode;
  27.             newNode.next = head;
  28.         }
  29.         size++;
  30.     }

  31.     public void delete(E data) {
  32.         if (head != null) {
  33.             if (head.data.equals(data)) {
  34.                 if (head.next == head) {
  35.                     head = null;
  36.                 } else {
  37.                     Node<E> temp = head;
  38.                     while (temp.next != head) {
  39.                         temp = temp.next;
  40.                     }
  41.                     temp.next = head.next;
  42.                     head = head.next;
  43.                 }
  44.                 size--;
  45.             } else {
  46.                 Node<E> current = head;
  47.                 Node<E> previous = null;
  48.                 do {
  49.                     if (current.data.equals(data)) {
  50.                         previous.next = current.next;
  51.                         size--;
  52.                         break;
  53.                     }
  54.                     previous = current;
  55.                     current = current.next;
  56.                 } while (current != head);
  57.             }
  58.         }
  59.     }

  60.     public boolean contains(E data) {
  61.         if (head == null) {
  62.             return false;
  63.         } else {
  64.             Node<E> temp = head;
  65.             do {
  66.                 if (temp.data.equals(data)) {
  67.                     return true;
  68.                 }
  69.                 temp = temp.next;
  70.             } while (temp != head);
  71.             return false;
  72.         }
  73.     }

  74.     public void modify(int index, E data) {
  75.         if (index < 0 || index >= size) {
  76.             throw new IndexOutOfBoundsException();
  77.         }
  78.         Node<E> temp = head;
  79.         for (int i = 0; i < index; i++) {
  80.             temp = temp.next;
  81.         }
  82.         temp.data = data;
  83.     }

  84.     @Override
  85.     public Iterator<E> iterator() {
  86.         return new Iterator<E>() {
  87.             private Node<E> current = head;
  88.             private int count = 0;

  89.             @Override
  90.             public boolean hasNext() {
  91.                 return count < size;
  92.             }

  93.             @Override
  94.             public E next() {
  95.                 E data = current.data;
  96.                 current = current.next;
  97.                 count++;
  98.                 return data;
  99.             }
  100.         };
  101.     }
  102. }
复制代码


这是一个基本的泛型循环链表类的实现。它包括了创建循环链表、插入新的结点、删除指定元素结点、查找指定元素结点、修改指定位置结点、遍历循环链表的功能。在这个实现中,我们使用了内部类Node来表示链表中的节点,并实现了Iterator接口来实现遍历功能。
小甲鱼最新课程 -> https://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,其中包含了创建循环链表、插入新的结点、删除指定元素结点、查找指定元素结点、修改指定位置结点、以及遍历循环链表的基本功能。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-3 11:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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