JAVA数据结构之线性表的链式存储结构——循环链表
来源:互联网 发布:征服者数据更新 编辑:程序博客网 时间:2024/05/01 13:42
循环链表的结构如下图所示:
说明:头结点的next指针指向循环链表中的第一个结点(下标为0),循环链表的最后一个结点的next指针指向第一个结点。下面是具体的实现代码。
1、结点类Node的实现代码如下所示:
public class Node {public Object data;//保存当前结点的数据public Node next;//指向下一个结点public Node(Object data){this.data = data;}}2、循环链表类CircleList的实现代码如下所示:
public class CircleList {private Node head;public CircleList(){head = new Node(0);head.next = null;}//在指定位置插入结点public boolean insert(Object data,int pos){boolean ret = (head != null) && (data != null) && (pos >= 0);if(ret){Node node = new Node(data);if(head.next == null){//插入的结点前,循环链表中没有结点。head.next = node;node.next = node;}else{if(pos >= (Integer)head.data){pos = (Integer)head.data;}Node currentNode = head.next;//若currentNode.next == head.next,就说明currentNode是最后一个结点for(int i = 0;(i < pos) && (currentNode.next != head.next);i++){currentNode = currentNode.next;}node.next = currentNode.next;currentNode.next = node;//插入位置的下标为0时if(pos == 0){head.next = node;}}head.data = (Integer)head.data + 1;}return ret;}//获取链表中下标为pos的结点public Object get(int pos){Object ret = null;if(head != null && pos >= 0 && pos < (Integer)head.data){Node node = head;//头结点//找到要删除的结点for(int i = 0;i<=pos;i++){node = node.next;}if(node != null){ret = node.data;}}return ret;}//删除链表中下标为pos的结点public Object delete(int pos){Object ret = null;if(head != null && pos >= 0 && pos < (Integer)head.data){Node node = head;//头结点Node currentNode = null;//要删除的结点//找到要删除结点的前一个结点for(int i = 0;i<pos;i++){node = node.next;}currentNode = node.next;//获取要删除结点的数据if(currentNode != null){ret = currentNode.data;}//要删除的结点是循环链表中的第一个结点if(head.next == currentNode){head.next = currentNode.next;}//删除结点node.next = currentNode.next;head.data = (Integer)head.data - 1;}return ret;}//清空链表public void clear(){if(head != null){head.data = 0;head.next = null;}}//注销链表public void destroy(){if(head != null){head = null;}}//获取链表中结点的个数public int length(){int ret = -1;if(head != null){ret = (Integer)head.data;}return ret;}//打印循环链表中的数据public void display(){if(head != null){Node node = head;for(int i = 0;i < (Integer)head.data;i++){node = node.next;System.out.print(node.data+" ");}}}}3、测试类Test的代码如下所示:
public class Test {public static void main(String[] args) {CircleList list = new CircleList();list.insert("结点1", 0);list.insert("结点2", 1);list.insert("结点3", 2);list.insert("结点4", 3);list.insert("结点5", 4);list.insert("结点6", 5);System.out.println("链表中的元素为:");list.display();System.out.println("\n链表中结点的个数:"+list.length());System.out.println("\n获取链表中下标为2的结点:"+list.get(2));System.out.println("删除链表中下标为0的结点:"+list.delete(0));System.out.println("链表中的元素为:");list.display();System.out.println("\n链表中结点的个数:"+list.length());list.clear();list.destroy();}}4、程序运行结果如下所示:
链表中的元素为:
结点1 结点2 结点3 结点4 结点5 结点6
链表中结点的个数:6
获取链表中下标为2的结点:结点3
删除链表中下标为0的结点:结点1
链表中的元素为:
结点2 结点3 结点4 结点5 结点6
链表中结点的个数:5
阅读全文
0 0
- JAVA数据结构之线性表的链式存储结构——循环链表
- JAVA数据结构之线性表的链式存储结构——双向链表
- JAVA数据结构之线性表的链式存储结构——单链表
- 线性表——链式存储结构之循环单链表
- 线性表——链式存储结构之循环双链表
- 数据结构:线性表之链式存储结构
- 【数据结构】线性表之链式存储结构
- 数据结构:线性表之链式存储结构
- 【数据结构】线性表的链式存储结构
- 【数据结构】-线性表的链式存储结构
- 【线性表】链式存储结构之:循环链表
- 线性表-链式存储结构之循环链表
- 数据结构笔记之线性表的链式存储结构
- Python 数据结构 之 线性表 的链式存储结构
- 线性表——链式存储结构之循环单链表的应用
- 大话数据结构四:线性表的链式存储结构(单向循环链表)
- 大话数据结构四:线性表的链式存储结构(单向循环链表)
- 数据结构与算法之——线性表的链式存储结构
- Camera安卓源码剖析-源码中的设计模式实例
- Core ML and Vision
- python 操作 mqsql 数据库
- Gcc编译出错处理--openssl 依赖问题
- MySql免安装版本配置步骤
- JAVA数据结构之线性表的链式存储结构——循环链表
- win10如何安装思科VPN client
- overflow-y在ios上滑动卡顿问题
- PopupWindonw的简单应用
- iOS学习之如何生成.mobileprovision文件
- 百度ai开发者大会讲了点什么
- openwrt 关于系统负载的数据理解
- spring使用AOP和自定义注解功能添加日志管理--实用
- 机器学习_1之dlib库