JAVA:实现Circle Linked List环形链表算法(附完整源码)

本文详细介绍了如何在JAVA中实现Circle Linked List,即环形链表的数据结构。从基本概念出发,讲解了环形链表的特性,并提供了完整的源代码示例,帮助读者深入理解环形链表的创建、遍历和操作方法。同时,文章还探讨了环形链表在JVM中的应用和优化策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JAVA:实现Circle Linked List环形链表算法

package com.thealgorithms.datastructures.lists;

public class CircleLinkedList<E> {
   

    private static 
下面是Java实现双向链表的完整源码: ```java public class DoublyLinkedList<T> { private Node head; private Node tail; private int size; public DoublyLinkedList() { head = null; tail = null; size = 0; } private class Node { T data; Node prev; Node next; public Node(T data) { this.data = data; prev = null; next = null; } } public int size() { return size; } public boolean isEmpty() { return size == 0; } public void addFirst(T data) { Node newNode = new Node(data); if (isEmpty()) { tail = newNode; } else { head.prev = newNode; newNode.next = head; } head = newNode; size++; } public void addLast(T data) { Node newNode = new Node(data); if (isEmpty()) { head = newNode; } else { tail.next = newNode; newNode.prev = tail; } tail = newNode; size++; } public void add(int index, T data) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException("Invalid index!"); } if (index == 0) { addFirst(data); return; } if (index == size) { addLast(data); return; } Node current = head; for (int i = 0; i < index - 1; i++) { current = current.next; } Node newNode = new Node(data); newNode.prev = current; newNode.next = current.next; current.next.prev = newNode; current.next = newNode; size++; } public T removeFirst() { if (isEmpty()) { throw new RuntimeException("Empty list!"); } T data = head.data; head = head.next; if (head == null) { tail = null; } else { head.prev = null; } size--; return data; } public T removeLast() { if (isEmpty()) { throw new RuntimeException("Empty list!"); } T data = tail.data; tail = tail.prev; if (tail == null) { head = null; } else { tail.next = null; } size--; return data; } public T remove(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Invalid index!"); } if (index == 0) { return removeFirst(); } if (index == size - 1) { return removeLast(); } Node current = head; for (int i = 0; i < index; i++) { current = current.next; } current.prev.next = current.next; current.next.prev = current.prev; size--; return current.data; } public T get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Invalid index!"); } Node current = head; for (int i = 0; i < index; i++) { current = current.next; } return current.data; } public void set(int index, T data) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Invalid index!"); } Node current = head; for (int i = 0; i < index; i++) { current = current.next; } current.data = data; } public boolean contains(T data) { Node current = head; while (current != null) { if (current.data.equals(data)) { return true; } current = current.next; } return false; } public void clear() { head = null; tail = null; size = 0; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("["); Node current = head; while (current != null) { sb.append(current.data); if (current.next != null) { sb.append(", "); } current = current.next; } sb.append("]"); return sb.toString(); } } ``` 代码中使用了嵌套类Node表示双向链表中的每个节点,包含一个数据域和两个指针域(prev和next),分别指向前驱节点和后继节点。 程序首先定义了一个DoublyLinkedList类,包含头节点head、尾节点tail和链表长度size三个属性。然后,程序实现了以下方法: - size:返回链表长度 - isEmpty:判断链表是否为空 - addFirst:在链表头部插入一个元素 - addLast:在链表尾部插入一个元素 - add:在指定位置插入一个元素 - removeFirst:删除链表头部元素 - removeLast:删除链表尾部元素 - remove:删除指定位置的元素 - get:获取指定位置的元素 - set:设置指定位置的元素 - contains:判断链表中是否包含指定元素 - clear:清空链表 - toString:将链表转换为字符串形式 以上是Java实现双向链表的完整源码,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源代码大师

赏点狗粮吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值