Java链表之输出(重点)

本文介绍了Java中链表数据结构的实现,包括添加、查询、修改、删除节点的方法,以及如何将链表转换为对象数组进行输出。核心方法如`toArray()`、`containsNode()`、`getNode()`、`setNode()`和`removeNode()`等进行了详细说明,并通过`Demo1`类的实例演示了链表的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*方法名称及作用 !!!!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]);
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值