Java版数据结构和算法学习笔记之线性结构链表篇

Java版数据结构和算法学习笔记之线性结构链表篇

1. 单链表

单链表还是比较简单的,直接上程序吧~

1.1 单链表常用功能实现代码

package main;

public class Node {
	
	public int data;
	public Node node;
	public Node next;
	
	//为结点赋值
	public void setData(int data) {
		this.data=data;
	}
	//获取结点值
	public int getData() {
		return data;
	}
	//获取下一结点
	public Node getNext() {
		return this.next;
	}
	//增加结点
	public void setNext(Node node) {
		Node currentNode=this;
		while(true) {
			if(currentNode.next==null) {
				break;
			}
			currentNode=currentNode.next;
		}
		currentNode.next=node;
	}
	//判断结点是否为最后一个
	public boolean isLast() {
		return next==null;
	}
	//删除下一结点
	public void removeNext() {
		//获取要删除的结点
		Node nextNode=this.next;
		if(nextNode.next==null) {
			this.next=null;
		}else {
			this.next=nextNode.next;
		}
		
	}
	//显示某结点后的所有结点信息
	public void show() {
		Node currentNode=this;
		while(true) {
			System.out.println(currentNode.getData()+" ");
			if(currentNode.getNext()==null) {
				break;
			}
			currentNode=currentNode.next;
		}
	}
	//在某一结点后插入一个结点
	public void insert(Node node) {
		Node currentNode=this;
		if(this.next==null) {
			this.next=node;
		}else {
			Node nextNode=this.next;
			this.next=node;
			node.setNext(nextNode);
		}
	}
}

1.2 测试代码

package test;
import main.Node;

public class NodeTest {
    public static void main(String[] args) {
    	Node n1=new Node();
    	Node n2=new Node();
    	Node n3=new Node();
    	Node n4=new Node();
    	n1.setData(1);
    	n2.setData(2);
    	n3.setData(3);
    	n4.setData(4);
    	//获取结点值并输出
    	//System.out.println(n1.getData());
    	//设置n2 为下一结点
    	n1.setNext(n2);
    	//输出下一结点值
    	//System.out.println(n1.getNext().getData());
    	//设置n3为下一结点
    	n1.setNext(n3);
    	//System.out.println(n1.getNext().getNext().getData());
    	//判断n3是否最后一个结点
    	//System.out.println(n3.isLast());
    	//删除下一结点
    	//n2.removeNext();
    	//查看是否删除成功
    	//System.out.println(n1.getNext().getData());
    	//n1.show();
    	//向n2后插入一个结点n4
    	n1.insert(n4);
    	n1.show();
    }
}

2. 循环链表

与单链表类似。

在这里插入图片描述

package main;

public class LoopNode {
	
	public int data;
	public LoopNode node;
	public LoopNode next=this;
	
	//为结点赋值
	public void setData(int data) {
		this.data=data;
	}
	//获取结点值
	public int getData() {
		return data;
	}
	//获取下一结点
	public LoopNode getNext() {
		return this.next;
	}
	//增加结点
	public void setNext(LoopNode node) {
		LoopNode NextnextNode=this.next;
		this.next=node;
		node.next=NextnextNode;
	}

	//删除下一结点
	public void removeNext() {
		//获取要删除的结点
		LoopNode nextNode=this.next;
		if(nextNode.next==null) {
			this.next=null;
		}else {
			this.next=nextNode.next;
		}
		
	}
	
}

package test;

import main.LoopNode;

public class LoopNodeTest {
    public static void main(String[] args) {
    	LoopNode n1=new LoopNode();
    	LoopNode n2=new LoopNode();
    	LoopNode n3=new LoopNode();
    	LoopNode n4=new LoopNode();
    	n1.setData(1);
    	n2.setData(2);
    	n3.setData(3);
    	n4.setData(4);
    	n1.setNext(n2);
    	n2.setNext(n3);
    	n3.setNext(n4);
    	System.out.println(n4.getNext().getData());
    	System.out.println(n1.getNext().getData());
    	System.out.println(n2.getNext().getData());
    	System.out.println(n3.getNext().getData());  	
    }
}

3. 双链表

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

就是二二二二婷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值