java实现简单链表

举报
bigsai 发表于 2021/02/03 01:01:39 2021/02/03
【摘要】 链表是由一个个节点连接起来的。首先链表的node类代码为: public class node { public int value;//节点的结果 node next;//下一个连接的节点 public node(){} public node(int value) { this.value=value; } public void display...

链表是由一个个节点连接起来的。首先链表的node类代码为:

public class node {
	public int value;//节点的结果
	node next;//下一个连接的节点
	public node(){}
	public node(int value)
	{
		this.value=value;
	}
	public void display()
	{
		System.out.println(value " ");
	}
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

其次是链表类,注意点已经写在注释
附上代码:

public class Linklist {
	node first;//头节点
	public Linklist()
	{
		first=null;
	}
	int length() {
		if(first==null) {return 0;} int n=1; node p=first; while(p.next!=null) {n ;p=p.next; }// return n;
	}
	/*
	 * 头部插入首先判断头节点是否为空,如果头节点为空,头节点的指向为新节点
	 * 如果头节点不为空,将新节点指向头节点,(暂时新节点在前,头节点指向在后)
	 * 将设头节点是 2 5 6** 那么新节点就是 3 2 5 6**
	 * 再将头节点指向新节点 那么头节点就是 3 2 5 6**
	 * 拥有了节点,就拥有了整个链表(个人认为)
	 */
	public void insertfirst(int a) //从头部插入
	{
		if(first==null)
		{first=new node(a);}
		else if(first!=null)
		{
		node p=new node(a);//新节点
		p.next=first;//节点指向first
		first=p;//first指向第一位。
		} }
	/*
	 * 尾部插入相对容易理解
	 * 首先还是要判断头是否为空
	 * 然后在找到最后一个节点,插入
	 */
	public void insertend(int b)//从尾部插入
	{
		if(first==null)
		{ first=new node(b);
		}
		else
		{
		node q=new node(b);//新节点
		node p=first;
		while(p.next!=null)
		{ p=p.next;
		}
		p.next=q;
		}
	}
	public void dispiay()//输出依然要判断头节点
	{
		if(first==null) {System.out.println("链表为空");return;} node p=new node();
		p.next=first; while(p.next!=null)
		{ p=p.next; System.out.println("value=" p.value);
		}
	}
}


  
 
  • 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
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

测试类和结果

public class test1 {
	public static void main(String[] args)
	{
		Linklist a=new Linklist();
	 a.insertfirst(20);
		a.insertfirst(7);
		a.insertfirst(8);
		a.insertfirst(9);
		a.insertfirst(155);
		a.insertend(70);
		a.insertend(75);
		a.insertend(780);
		a.dispiay();
		System.out.println(a.length());
	}
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

输出结果为:
value=155
value=9
value=8
value=7
value=20
value=70
value=75
value=780
8

如果对后端、爬虫等感性趣欢迎关注我的个人公众号交流:bigsai

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

原文链接:bigsai.blog.csdn.net/article/details/79681751

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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