java实现单链表

class Node {
	
	int data;  //节点内容
	Node next; //下一个节点
	
	public Node(int value) {
		this.data = value;
	}
	
	//为节点追加节点  -- 需要看这个节点后还有没有其他节点
	//有的话 继续向后找 直到找到最后一个节点为止
	public void append(Node node) {
		Node currentNode = this;
		while(true) {
			Node nextNode = currentNode.next;
			currentNode = nextNode;
			if (nextNode == null) {
				break;
			}
		}
		currentNode = node;
	}
	
	//获取下一个节点
	public Node next() {
		return this.next;
	}
	
	//获取节点的数据
	public int getData() {
		return this.data;
	}
	
	//当前节点是否是最后一个结点 -- 下一个是否为空
	public boolean isLast() {
		return next == null;
	}
	
	//删除下一个节点
	public void removeNext() {
		Node newNext = next.next;
		this.next = newNext;
	}
	
	//显示所有节点信息
	public void show() {
		Node currentNode = this;
		while (true) {
			System.out.println(currentNode.data + " ");
			currentNode = currentNode.next;
			if (currentNode == null) {
				break;
			}
		}
	}
	
	//插入一个新的节点
	public void insert(Node node) {
		Node nextNext = next;
		this.next = node;
		node.next = nextNext;
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值