2017/08/27 环形链表的实现

本文介绍了一个简单的循环链表实现,包括节点的定义、基本操作如插入、删除、遍历等方法,并提供了完整的Java代码示例。

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



// 循环链表
public class CircleLinkList {

// 节点
class Link {
int data;
Link next;

public Link(int temp) {
data = temp;
next = null;
}

public void display() {
System.out.print(data+" ");
}
}

// 当前节点
Link current = null;
// 当前节点的前一个节点
Link prev = null;
// 节点个数
int size = 0;

// 判断是否为空
public boolean isEmpty() {
return (size == 0);
}

// 遍历链表
public void display() {
if (!isEmpty()) {
int count = 0;
while ((count++) < size) {
current.display();
nextLink();
}
}
else
System.out.println("数据为空!");
}

// 获取当前节点
public Integer getCurrent() {
return isEmpty() ? null : current.data;
}

// 节点移动一个位置
public boolean nextLink() {
return (prev == null) ? false : ((current = (prev = current).next) != null);
}

// 获取节点个数
public int Size() {
return size;
}

// 插入节点
public void insertAfter(int a) {
Link newLink = new Link(a);
if (isEmpty()) {
current = newLink;
size = 1;
return;
}
if (prev == null) 
(prev = newLink).next = current;
else
newLink.next = current.next;
current.next = newLink;
nextLink();
size++;
}

// 删除当前节点 
public Integer deleteCurrent() {
if (isEmpty()) 
return null;
int temp = current.data;
if (current.next != null) 
if (current.next == prev) 
prev = (prev.next = null);
else
prev.next = current.next;
current = current.next;
size--;
return temp;
}

// 查找某节点
public boolean find(int temp) {
int count = 0;
while ((count++) <= size) {
if (current.data == temp) 
return true;
nextLink();
}
return false;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值