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> {
private E data;
private Node<E> next;
public Node(E data) {
this.data = data;
}
}
public CircularLinkedList() {
head = null;
size = 0;
}
public void insert(E data) {
Node<E> newNode = new Node<>(data);
if (head == null) {
head = newNode;
head.next = head;
} else {
Node<E> temp = head;
while (temp.next != head) {
temp = temp.next;
}
temp.next = newNode;
newNode.next = head;
}
size++;
}
public void delete(E data) {
if (head != null) {
if (head.data.equals(data)) {
if (head.next == head) {
head = null;
} else {
Node<E> temp = head;
while (temp.next != head) {
temp = temp.next;
}
temp.next = head.next;
head = head.next;
}
size--;
} else {
Node<E> current = head;
Node<E> previous = null;
do {
if (current.data.equals(data)) {
previous.next = current.next;
size--;
break;
}
previous = current;
current = current.next;
} while (current != head);
}
}
}
public boolean contains(E data) {
if (head == null) {
return false;
} else {
Node<E> temp = head;
do {
if (temp.data.equals(data)) {
return true;
}
temp = temp.next;
} while (temp != head);
return false;
}
}
public void modify(int index, E data) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
Node<E> temp = head;
for (int i = 0; i < index; i++) {
temp = temp.next;
}
temp.data = data;
}
@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;
}
};
}
}
这是一个基本的泛型循环链表类的实现。它包括了创建循环链表、插入新的结点、删除指定元素结点、查找指定元素结点、修改指定位置结点、遍历循环链表的功能。在这个实现中,我们使用了内部类Node来表示链表中的节点,并实现了Iterator接口来实现遍历功能。 当使用Java编程实现循环链表的泛型类时,可以按照以下方式设计:
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);
}
}
在上述代码中,我们定义了一个泛型类 CircularLinkedList,其中包含了创建循环链表、插入新的结点、删除指定元素结点、查找指定元素结点、修改指定位置结点、以及遍历循环链表的基本功能。
页:
[1]