/*方法名称及作用 !!!!2
public 数据类型 toArray() 将链表以对象数组的形式返回
需要增加一个返回的数组属性内容。 private String [] retArray;
*/
class Link{
private class Node{
private String data;
private Node next;
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);
}// 没有后续节点 , 返回false
else{
return false;
}
}
}
/* public void printNode(Node node){ //输出数据
System.out.print(this.data+"->");
if(node.next != null)
this.next.printNode(next);
}*/
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(previous.next, data);
}
}
public String [] toArrayNode(){//转化为对象数组进行输出
Link.this.retArray[Link.this.foot ++] = this.data;
if(this.next != null){
this.next.toArrayNode();
}
return null;
}
}
//===============以上为内部类============
private Node root;
private int foot = 0; // 索引
private int count = 0;//保存元素个数
private String [] retArray;
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); //增加判断转到Node()类
}
this.count++;
}
public int size(){ //取得保存的数据量
return this.count;
}
public boolean isEmpty(){ //判断链表是否为空
return this.count==0;
}
public boolean contains(String data){ //查询
//没有查询结果或者根节点没数据
if(data == null || this.root == null)
return false;
return this.root.containsNode(data);
}
public String get(int index){ //查询指定索引的内容
this.foot = 0;
if(index > count)
return null;//没有数据
else
return this.root.getNode(index);
}
public void set(int index,String data){ //修改指定索引内容
this.foot = 0;
if(index > this.count)
return ; //结束
else
this.root.setNode(index,data);
}
/* public void print(){ // 输出数据
if(this.root == null)
return ;
else
this.root.printNode(root);
}*/
public void remove(String data){ // 删除指定内容的节点
if(this.contains(data)){ //contains保证数据存在
if(data.equals(this.root.data)){ //删除的为根节点
this.root = this.root.next;
}else{ //不是根节点,从第二个节点开始
this.root.next.removeNode(this.root, data);
}
this.count --; //删除后个数-1
}
}
public String [] toArray(){ //转化为对象数组进行输出(重要)
if(this.root == null){
return null;
}else{
this.foot = 0;
this.retArray = new String[this.count]; //开辟内存
this.root.toArrayNode();
}
return this.retArray;
}
}
public class Demo1 {
public static void main(String[] args) {
Link link = new Link();
link.add("a");
link.add("b");
link.add(null );
link.add("c");
String [] data = link.toArray();
for(int i = 0; i < data.length; i++){
System.out.println(data[i]);
}
}
}
Java链表之输出(重点)
最新推荐文章于 2025-03-18 17:57:36 发布