Java实现循环单链表……
2015-10-06 16:31
645 查看
相比于C和C++,Java中没有了“结构”,因此就要使用类来描述“结构”,同样的,在实现链表时,就可以使用类来定义节点,然后进行相应的操作,以下是我实现循环单链表的代码,循环单链表和单链表的区别在于最后一个节点的判断,整体不难……
以下是测试代码:
以上仅仅是我个人对循环单链表的理解,有任何疏漏之处欢迎在评论区指明,谢谢!
标记为原创的博文均为本人辛苦码字所得,谢绝抄袭,转载请注明出处,新浪微博私信艾特:http://weibo.com/nieganghust
class CNode{ private Object data; //定义数据域 private CNode nextCNode; //定义下一个节点 private CNode head=null;//定义头节点 public Object getData() { return data; } public void setData(Object data) { this.data = data; } public CNode getNextCNode() { return nextCNode; } public void setNextCNode(CNode next) { this.nextCNode = next; } public void initCycleList(Object data){ //循环链表的初始化 head=new CNode(); head.setData(data); head.setNextCNode(head); } public void insertCycleListTail(Object data){ CNode inCNode=new CNode(); inCNode.setData(data); if (head==head.getNextCNode()) { //如果原链表只有一个节点,直接插入 head.setNextCNode(inCNode); inCNode.setNextCNode(head); }else { //原链表不止一个节点 CNode temp=head; //创建临时节点 while (head!=temp.getNextCNode()) { //遍历循环链表,找到最后一个节点 temp=temp.getNextCNode(); } temp.setNextCNode(inCNode); //插入节点 inCNode.setNextCNode(head); } } //求循环链表的长度 public int cycleListSize(){ CNode temp=head; int size=0; while (temp.getNextCNode()!=head) { size++; temp=temp.getNextCNode(); } return size; } //判断循环链表中是否存在某个元素 public Boolean isContain(Object data){ CNode temp=head; while (temp.getNextCNode()!=head) { if (temp.getData().equals(data)) { return true; } temp=temp.getNextCNode(); } return false; } //获取循环链表中第i个位置的元素 public CNode getCNode(int i){ if (i<0||i>cycleListSize()) { System.out.println("输入有误"); return null; }else { int count=0; CNode temp=head; CNode retCNode=new CNode(); while (head!=temp.getNextCNode()) { if (count==i) { retCNode.setData(temp.getData()); break; } temp=temp.getNextCNode(); count++; } return retCNode; } } //打印循环链表 public void printCycleList(){ CNode temp=head; while (head!=temp.getNextCNode()) { System.out.print(temp.getData()+" "); temp=temp.getNextCNode(); } System.out.println(); } }
以下是测试代码:
public class CycleList { public static void main(String[] args){ CNode testCNode=new CNode(); testCNode.initCycleList(100); testCNode.insertCycleListTail(0); testCNode.insertCycleListTail(1); testCNode.insertCycleListTail(2); testCNode.insertCycleListTail(3); testCNode.printCycleList(); System.out.println(testCNode.isContain(3)); System.out.println(testCNode.getCNode(2).getData()); System.out.println(testCNode.cycleListSize()); } }
以上仅仅是我个人对循环单链表的理解,有任何疏漏之处欢迎在评论区指明,谢谢!
标记为原创的博文均为本人辛苦码字所得,谢绝抄袭,转载请注明出处,新浪微博私信艾特:http://weibo.com/nieganghust
相关文章推荐
- java并发之ConcurrentHashMap
- java线程详解(三)
- Spring3.2AOP实现需要添加的三个包
- JAVA深入研究——Method的Invoke方法
- eclipse 断点调试
- Java的运行原理--jvm
- java中抽象类与接口
- 关于jsp利用EL和struts2标签来遍历ValueStack的东东 ------> List<Map<K,V>> 以及 Map<K,<List<xxx>>> 的结构遍历
- java 中static final关键字
- eclipse中倒入项目时,报关于.classpath的错误
- JavaBean入门 - 概念和简单例子
- Java bean类方法命名上规范
- 百度地图---获取当前位置出现的问题--Caused by: java.lang.IllegalStateException: no found the liblocSDK4d.so file
- Java字符串详解
- Java版线性表的链式存储和实现
- win10的java环境该怎么配置?java环境变量配置介绍
- Spring issue checking links
- JavaBean 属性命名规范特例
- Struts 2 ——ActionContext和ServletActionContext区别
- java实现简单的计算器类实例