java创建链表

举报
花狗Fdog 发表于 2021/03/26 00:31:00 2021/03/26
【摘要】 package number; public class chapter02 { public static void main(String[] args) { //生成5个节点 Node Head = new Node(0,null); //产生头节点 Node End = new Node(0,null); //产生尾节点 for(int i ...
package number;
public class chapter02 {
	public static void main(String[] args) {
		//生成5个节点
		Node Head = new Node(0,null); //产生头节点
		Node End = new Node(0,null);  //产生尾节点
		for(int i = 0;i<5;i++){ //创建节点 Node body = new Node(); Head.setSum(i+1); if(Head.next==null){ //只有头结点 Head.next = body; }else{ End.next = body; } body.setData(i, null); End = body;
		}
		while(Head!=null){ Head.getData(); Head = Head.next;
		}
	}
}
//定义链表
 class Node{
	private int data; //节点数据
	public Node next; //下一个节点
	public Node(){}
	public Node(int data,Node next){
		this.data = data;
		this.next = next;	
	}
	public int isNull(){
		return this.data;
	}
	public void setData(int data,Node next){
		this.data = data;
		this.next = next;
	}
	public void setSum(int Sum){
		this.data = Sum;
	}
	public void getData(){
		System.out.println(this.data);
	}
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

文章来源: blog.csdn.net,作者:花狗Fdog,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/Fdog_/article/details/106275353

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。