中易网

java 链表实现(测试是否有环)

答案:1  悬赏:10  
解决时间 2021-02-08 10:08
java 链表实现(测试是否有环)
最佳答案
package com.bb.bbs; import java.util.ArrayList; class Node{ Node next; String data;//下一节点 public static int maxs = 0;//getSize() 最大数量 public static int maxg = 0;//getArray()最大数量 public static int maxp = 0;//printNode()打印节点最大数量 public static int maxc = 0;//contains()最大数量 public Node(String data){ this.data = data; } public void addNode(Node node){ if(this.next==null){ this.next = node; }else{ this.next.addNode(node); } } public boolean removeNode(Node previous,String data){ if(this.data.equals(data)){ previous.next = this.next; return true; }else{ return this.next.removeNode(this, data); } } public boolean contains(String data){ maxc++; if(maxc==10){ return false; } if(this.data.equals(data)){ return true; } if(this.next == null){ return false; }else{ return this.next.contains(data); } } public void printNode(){ maxp++; if(maxp==10){ return; } if(this.next!=null){ System.out.println(this.next.data); this.next.printNode(); } } public Node findNode(String data){ if(this.data.equals(data)){ return this; }else{ return this.next.findNode(data); } } public int getSize(int currentNum){ maxs++; if(maxs==10){ return 10; } if(this!=null){ currentNum++; } if(this.next!=null){ return this.next.getSize(currentNum); }else{ return currentNum; } } public void getArray(ArrayList tArrayList){ maxg++; if(maxg==10){ return; } tArrayList.add(this.data); if(this.next!=null){ this.next.getArray(tArrayList); } } } class Link{ Node root; public void add(String data){ if(data==null){ return; } if(root ==null){ root = new Node(data); }else{ root.addNode(new Node(data)); } } public boolean remove(String data){ if(root.data.equals(data)){ root = root.next; return true; }else{ return this.root.next.removeNode(root, data); } } public boolean contains(String data){ if(root.data.equals(data)){ return true; }else{ return root.contains(data); } } public void print(){ if(root!=null){ System.out.println(root.data); root.printNode(); } } public Node find(String data){ if(contains(data)){ if(this.root.data.equals(data)){ return root; }else{ return root.findNode(data); } } return null; } public int size(){ if(root.next ==null){ return 1; }else{ return root.next.getSize(1); } } public void getArray(ArrayList tArrayList){ if(root!=null){ tArrayList.add(root.data); } if(root.next!=null){ root.next.getArray(tArrayList); } } } public class LinkList { public static void main(String args[]){ //1、增加链表节点 Link tLink = new Link(); tLink.add("A"); tLink.add("B"); tLink.add("C"); tLink.add("D"); tLink.print(); //2、形成环 A->B->C->A->B->C->A->B->C...... 无限循环 Node fnodeA = tLink.find("A"); Node fnodeC = tLink.find("C"); //如果有一个为空 if(fnodeA==null || fnodeC==null){ System.out.println("没有找到元素!"); fnodeC.next = fnodeA;//出现环 } int linksize = tLink.size(); System.out.println("链表大小为:"+linksize); ArrayList tArrayList = new ArrayList(); tLink.getArray(tArrayList); for(Object o : tArrayList){ String str = o.toString(); System.out.println(str); } boolean flag = false; int circleLen = 0; //检验 "环" 是否存在 for(int i=0;i
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
A4纸叠成正方体和卷成圆柱那个支撑力更大?为
英语:"动物的狂欢"怎么说?
国家宝藏2的拍摄地点有哪些
爱你宝贝我想知道这个在什么地方
把手机连同电池一起放到冰箱里冷冻,手机会坏
phnix 空气然热泵怎么样?
我儿子农历3月初3上午10点二十分出生,重6斤6
lol换肤软件有哪些不封号的那种
经济学专业考研情况是怎么样的?
我的笔记本cpu,显卡温度总是70左右,怎么回
如何让水底的针自动漂浮到水面?如何让观音像
南海市新力电池有限公司这个地址在什么地方,
汽车有遇难自动报警系统吗
现在,我只有你陪在我身边 英语翻译
丽莹美容美体中心在哪里啊,我有事要去这个地
推荐资讯
中国邮政(宁蒗彝族自治县邮政支局)地址在什么
许多工作都要求工作经验,请问如何证明有工作
华侨大学数学与应用数学在哪个校区
大连机场地勤待遇怎么样
国有投资公司拨付企业基础设施配套补助应该计
找么吃在哪里啊,我有事要去这个地方
延边州水利水电勘测设计研究院在哪里啊,我有
山东重骑摩托车400cc
给个个性签名,给20分~
竹山町地址在哪,我要去那里办事
骐达08款的发动机是不是进口的?
梦见一池水一下被抽完
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?