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);
}
}
/**
* 从root开始寻找,目的是移除一个节点
*/
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;
}
}
/**
* 将节点里所有值封装到一个ArrayList中
*/
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("没有找到元素!");
}else{
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<linksize;i++){
for(int j=i;j<linksize;j++){
if(j!=i && tArrayList.get(j).toString().equals(tArrayList.get(i).toString())){
circleLen = j-i;
flag = true;
break;
}
}
}
if(flag){
System.out.println("有环,环的长度为:"+circleLen);
}else{
System.out.println("无环");
}
//3、移除一个节点
tLink.remove("B");
//4、移除后打印所有节点
tLink.print();
//打印链表长度
tLink.root.maxs = 0;
System.out.println(tLink.size());
//5、是否包含有C这个节点
System.out.println(tLink.contains("C"));
}
}
java 链表实现(测试是否有环)
最新推荐文章于 2022-12-29 18:03:39 发布