class Link{
//-------------------------- 内部类------------------------------
private class Node{
private String data;
private Node next;
private int foot = 0;
public Node(String data){
this.data = data;
}
public void addNode(Node newNode){
if (this.next == null){
this.next = newNode;
}else{
this.next.addNode(newNode);
}
}
public boolean containsNode(String data){
if (data.equals(this.data)){ //如果相等
return true;
}else{
if (this.next != null){ //有后续节点
return this.next.containsNode(data);
}else{//无后续节点
return false;
}
}
}
public String getNode(int index){
if (Link.this.foot++ == index){
return this.data;
}else{
return this.next.getNode(index);
}
}
public void setNode(int index, String data){
if (Link.this.foot++ == index){
this.data = data;
}else{
this.next.setNode(index, data);
}
}
public void removeNode(Node previous, String data){
if (data.equals(this.data)){
previous.next = this.next;
}else{
this.next.removeNode(this, data);
}
}
public void toArrayNode(){
Link.this.retArray[Link.this.foot++] = this.data;
if (this.next != null){ //如果 有后续节点,继续递归访问
this.next.toArrayNode();
}
}
}
//-------------------------- Link 属性-------------------------------
private Node root;
private int count;
private int foot;
private String[] retArray;
//-------------------------- Link 属性-------------------------------
public void add(String data){
if (data == null){
return;
}
Node newNode = new Node(data);
if (this.root == null){
this.root = newNode;
}else{
this.root.addNode(newNode);
}
this.count++;//添加节点后累加
}
public int size(){
return this.count;
}
public boolean isEmpty(){
return root == null ? true : false;
}
public boolean contains(String data){
if (data == null || this.root == null){
return false;
}
return this.root.containsNode(data);
}
public void print(){
Node temp = this.root;
while(temp != null){
System.out.println(temp.data);
temp = temp.next;
}
System.out.println("===========================");
}
public String getByIndex(int index){
if (index > this.count){ //索引越界返回 null
return null;
}
this.foot = 0; //
return this.root.getNode(index); //查询过程交给 Node 类
}
public void setByIndex(int index,String data)
{
if (index > this.count){
return;
}
//this.foot = 0;
this.root.setNode(index, data);
}
public void remove(String data){
if (data.equals(this.root.data)){
this.root = this.root.next;
}else{
this.root.next.removeNode(this.root, data);
}
this.count--;
}
public String[] toArray(){
if (this.root == null){
return null;
}
this.foot = 0;//下标临时变量设置。toArrayNode 里要用。
this.retArray = new String[this.count];
this.root.toArrayNode();
return this.retArray;
}
}
public class Chain{
public static void main(String[] args){
Link all = new Link();
//System.out.println(all.isEmpty());
all.add("Hello");
all.add("world");
all.add("你好");
all.add("呆板");
all.add(null);
all.setByIndex(1,"xxxxxx");
System.out.println(all.contains("Hello"));
System.out.println(all.getByIndex(3));
//all.print();
//all.remove("呆板");
//all.print();
//System.out.println(all.size());
String str[] = all.toArray();
int len = str.length;
for (int i=0; i < len; i++)
{
System.out.println(str[i]);
}
}
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史