用java编写,并完成所有测试类
1.请编程完成泛型类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, Node<E> next) {
this.data = data;
this.next = next;
}
}
public CircularLinkedList() {
head = null;
size = 0;
}
public void insert(E element) {
if (head == null) {
head = new Node<>(element, null);
head.next = head;
} else {
Node<E> newNode = new Node<>(element, head.next);
head.next = newNode;
}
size++;
}
public void delete(E element) {
if (head == null) {
return;
}
if (head.data.equals(element)) {
if (size == 1) {
head = null;
} else {
Node<E> current = head;
while (!current.next.data.equals(element)) {
current = current.next;
}
current.next = current.next.next;
if (element.equals(head.data)) {
head = current.next;
}
}
size--;
} else {
Node<E> current = head;
while (!current.next.data.equals(element)) {
current = current.next;
if (current.next == head) {
return;
}
}
current.next = current.next.next;
size--;
}
}
public Node<E> find(E element) {
Node<E> current = head;
if (head != null) {
do {
if (current.data.equals(element)) {
return current;
}
current = current.next;
} while (current != head);
}
return null;
}
public void modify(int index, E element) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
Node<E> current = head;
for (int i = 0; i < index; i++) {
current = current.next;
}
current.data = element;
}
@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;
}
};
}
}
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();
}
}
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();
}
}
页:
[1]