Java循环链表
java请编程完成泛型类CircularLinkedList(循环链表),CircularLinkedList的基本功能设计要求:创建循环链表、插入新的结点、删除指定元素结点、查找指定元素结点、修改指定位置结点、遍历循环链表。并写测试类 import java.util.Iterator;public class CircularLinkedList<E> implements Iterable<E> {
private Node<E> head;
private int size;
private static class Node<E> {
E data;
Node<E> next;
Node(E data) {
this.data = data;
}
}
public void insert(E data) {
if (head == null) {
head = new Node<>(data);
head.next = head;
} else {
Node<E> newNode = new Node<>(data);
newNode.next = head.next;
head.next = newNode;
}
size++;
}
public void delete(E data) {
if (head == null) {
return;
}
Node<E> current = head;
Node<E> previous = null;
do {
if (current.data.equals(data)) {
if (previous != null) {
previous.next = current.next;
} else {
head = current.next;
}
size--;
return;
}
previous = current;
current = current.next;
} while (current != head);
}
public Node<E> find(E data) {
Node<E> current = head;
do {
if (current.data.equals(data)) {
return current;
}
current = current.next;
} while (current != head);
return null;
}
public void modify(int index, E newData) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index out of bounds");
}
Node<E> current = head;
for (int i = 0; i < index; i++) {
current = current.next;
}
current.data = newData;
}
@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;
}
};
}
}
// 测试类
public class CircularLinkedListTest {
public static void main(String[] args) {
CircularLinkedList<Integer> list = new CircularLinkedList<>();
list.insert(1);
list.insert(2);
list.insert(3);
for (Integer data : list) {
System.out.println(data);
}
list.delete(2);
for (Integer data : list) {
System.out.println(data);
}
list.modify(1, 5);
for (Integer data : list) {
System.out.println(data);
}
}
}
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
}
}
页:
[1]