鱼C论坛

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

用java编写,并完成所有测试类

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

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

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

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

使用道具 举报

发表于 2023-11-13 10:48:30 | 显示全部楼层
  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, Node<E> next) {
  9.             this.data = data;
  10.             this.next = next;
  11.         }
  12.     }

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

  17.     public void insert(E element) {
  18.         if (head == null) {
  19.             head = new Node<>(element, null);
  20.             head.next = head;
  21.         } else {
  22.             Node<E> newNode = new Node<>(element, head.next);
  23.             head.next = newNode;
  24.         }
  25.         size++;
  26.     }

  27.     public void delete(E element) {
  28.         if (head == null) {
  29.             return;
  30.         }
  31.         if (head.data.equals(element)) {
  32.             if (size == 1) {
  33.                 head = null;
  34.             } else {
  35.                 Node<E> current = head;
  36.                 while (!current.next.data.equals(element)) {
  37.                     current = current.next;
  38.                 }
  39.                 current.next = current.next.next;
  40.                 if (element.equals(head.data)) {
  41.                     head = current.next;
  42.                 }
  43.             }
  44.             size--;
  45.         } else {
  46.             Node<E> current = head;
  47.             while (!current.next.data.equals(element)) {
  48.                 current = current.next;
  49.                 if (current.next == head) {
  50.                     return;
  51.                 }
  52.             }
  53.             current.next = current.next.next;
  54.             size--;
  55.         }
  56.     }

  57.     public Node<E> find(E element) {
  58.         Node<E> current = head;
  59.         if (head != null) {
  60.             do {
  61.                 if (current.data.equals(element)) {
  62.                     return current;
  63.                 }
  64.                 current = current.next;
  65.             } while (current != head);
  66.         }
  67.         return null;
  68.     }

  69.     public void modify(int index, E element) {
  70.         if (index < 0 || index >= size) {
  71.             throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
  72.         }
  73.         Node<E> current = head;
  74.         for (int i = 0; i < index; i++) {
  75.             current = current.next;
  76.         }
  77.         current.data = element;
  78.     }

  79.     @Override
  80.     public Iterator<E> iterator() {
  81.         return new Iterator<E>() {
  82.             private Node<E> current = head;
  83.             private int count = 0;

  84.             @Override
  85.             public boolean hasNext() {
  86.                 return count < size;
  87.             }

  88.             @Override
  89.             public E next() {
  90.                 E data = current.data;
  91.                 current = current.next;
  92.                 count++;
  93.                 return data;
  94.             }
  95.         };
  96.     }
  97. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

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

    // 创建循环链表
    public void createCircularLinkedList(T data) {
        Node<T> newNode = new Node<>(data);
        if (head == null) {
            head = newNode;
            head.next = head;
        } else {
            Node<T> temp = head;
            while (temp.next != head) {
                temp = temp.next;
            }
            temp.next = newNode;
            newNode.next = head;
        }
        size++;
    }

    // 插入新的结点
    public void insertNode(T data, int position) {
        if (position < 0 || position > size) {
            throw new IndexOutOfBoundsException("Invalid position for insertion");
        }

        Node<T> newNode = new Node<>(data);
        if (position == 0) {
            newNode.next = head;
            Node<T> temp = head;
            while (temp.next != head) {
                temp = temp.next;
            }
            temp.next = newNode;
            head = newNode;
        } else {
            Node<T> temp = head;
            for (int i = 0; i < position - 1; i++) {
                temp = temp.next;
            }
            newNode.next = temp.next;
            temp.next = newNode;
        }
        size++;
    }

    // 删除指定元素结点
    public void deleteNode(T data) {
        if (head == null) {
            return;
        }

        if (head.data.equals(data)) {
            if (head.next == head) {
                head = null;
            } else {
                Node<T> temp = head;
                while (temp.next != head) {
                    temp = temp.next;
                }
                temp.next = head.next;
                head = head.next;
            }
            size--;
        } else {
            Node<T> prev = head;
            Node<T> current = head.next;
            while (current != head) {
                if (current.data.equals(data)) {
                    prev.next = current.next;
                    size--;
                    return;
                }
                prev = current;
                current = current.next;
            }
        }
    }

    // 查找指定元素结点
    public int findNode(T data) {
        int index = 0;
        Node<T> temp = head;
        do {
            if (temp.data.equals(data)) {
                return index;
            }
            temp = temp.next;
            index++;
        } while (temp != head);
        return -1;
    }

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

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

    // 遍历循环链表
    public void traverseList() {
        if (head == null) {
            return;
        }
        Node<T> temp = head;
        do {
            System.out.print(temp.data + " ");
            temp = temp.next;
        } while (temp != head);
        System.out.println();
    }
}
[/code]

[/code]java
public class CircularLinkedListTest {
    public static void main(String[] args) {
        CircularLinkedList<Integer> circularList = new CircularLinkedList<>();

        // 创建循环链表
        circularList.createCircularLinkedList(1);
        circularList.createCircularLinkedList(2);
        circularList.createCircularLinkedList(3);

        // 遍历循环链表
        circularList.traverseList();

        // 插入新的结点
        circularList.insertNode(4, 2);
        circularList.traverseList();

        // 删除指定元素结点
        circularList.deleteNode(2);
        circularList.traverseList();

        // 查找指定元素结点
        int position = circularList.findNode(3);
        System.out.println("Position of 3: " + position);

        // 修改指定位置结点
        circularList.modifyNode(5, 0);
        circularList.traverseList();
    }
}
[/code]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 03:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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