Java单链表的增,删,改,插 超详解!!!

Java中的链表

简单的讲一下Java中如何创建一个链表以及对其进行基本的增删改插。
数据结构如下:
一个节点Node中包含有数据data和指向下一个节点的链表类指针next
在这里插入图片描述
其中尾节点的next为空(null),以表示链表结束。

链表的定义

首先我们需要定义一个链表类,这里我自己写的valint整型数类型,你可以根据自己的需求更换数据类型(char,String,double,etc)。
代码如下:

class LinkNode//链表定义
	{
		public int val;//值域
		LinkNode next = null;//指向下一个节点
		
		public LinkNode(int val)//对值域进行传值
			{
				this.val=val;
			}
	}
LinkNode head = null;//得到头节点
LinkNode current = null;//当前节点(工具人节点)

链表的创建

有了基本的链表定义后,我们就可以创建一个链表了。
在主函数中我们将使用for循环一步完成创建,如不明白请移步至完整代码
在这里插入图片描述

代码如下:

public void createList(int val)//链表的创建
	{
		if(head == null)//空链表时
		{
			head = new LinkNode(val);
			current = head;
		}
		else//链表已经存在元素了
		{
			LinkNode node = new LinkNode(val);
			current.next = node;
			current = current.next;
		}
	}

链表的遍历

依次访问每一个节点,输出每一个节点的val,然后执行current= current.next以实现当前指针的下移。
代码如下:

public void print()//打印函数(遍历链表)
	{
		if(head == null)//防止链表为空
		throw new IllegalArgumentException("您还未创建链表!");
		current = head;
		int i = 1;
		while(current != null)
		{
			System.out.println("第"+i+"号元素:"+" "+current.val);
			i++;
			current = current.next;
		}
	}

链表元素添加

接下来我们为链表添加元素,添加方法为在其表尾加一个节点。
如果要实现在其表头其他位置添加元素,详见链表元素插入
代码如下:

public void add()//添加函数
	{
		if(head == null)//防止链表为空
		throw new IllegalArgumentException("您还未创建链表!");
		print();//添加的链表展示
		System.out.println("请输入你想添加的元素:");
		int num;
		Scanner sc = new Scanner(System.in);
		num = sc.nextInt();
		sc.close();
		current = head;
		while(current.next != null)//得到尾节点
		{
			current = current.next;
		}
		LinkNode node = new LinkNode(num);
		current.next = node;
		System.out.println("添加后:");
		print();//添加后的链表展示
	}

链表元素插入


这里我们需要分情况讨论,如果你是在表头head前插入元素的话,此时head将会发生改变
在这里插入图片描述
当你在其他位置插入时,需要把插入前后的节点Node连起来。
在这里插入图片描述
代码如下:

public void insert()//插入函数
	{
		if(head == null)//防止链表为空
		throw new IllegalArgumentException("您还未创建链表!");
		print();//插入前的展示
		System.out.println("请输入你想插入的元素的值和位置(PS:插入功能指插入到某个元素之前):");
		int num,j;
		Scanner sc = new Scanner(System.in);
		num = sc.nextInt();//读取你要插入的元素的val
		j = sc.nextInt();//读取你要插入的位置
		sc.close();
		current = head;
		LinkNode previousNode = null;//用于记录current的前一个节点
		for(int i=1;current != null;i++)
		{
			if(j == 1)//当你要插入头节点时
			{
				LinkNode node = new LinkNode(num);
				node.next = head;//节点连接
				head = node;//头节点转移
				System.out.println("插入后:");
				print();
				break;
			}
			if(i == j)//当你需要插入其他节点之间时
			{
				LinkNode node = new LinkNode(num);
				previousNode.next = node;//前一个节点与新节点相连
				node.next = current;//新节点的next指向插入位置
				System.out.println("插入后:");
				print();
				break;
			}
			previousNode = current;
			current = current.next; 
		}
	}

链表元素删除

和链表插入一样,我们同样需要对删除的位置进行讨论。
这个功能的实现,需要我们找到需要删除的节点具体位置。然后连接前一个节点和后一个节点即可。如图所示:
在这里插入图片描述
代码如下:

public void delete()//插入函数
	{
		if(head == null)//防止链表为空
		throw new IllegalArgumentException("您还未创建链表!");
		print();//删除前展示
		System.out.println("请输入你想删除的元素的位置:");
		int j;
		Scanner sc = new Scanner(System.in);
		j = sc.nextInt();
		sc.close();
		current = head;
		LinkNode previousNode = null;//声明一个先前节点
		for(int i=1;current != null;i++)
		{
			if(j == 1)//需要删除头节点时
			{
				head = head.next;//头节点下移
				System.out.println("删除后:");
				print();
				break;
			}
			if(i == j)//删除其他位置
			{
				previousNode.next = current.next;//先前节点与下一个节点相连
				System.out.println("删除后:");
				print();
				break;
			}
			previousNode = current;
			current = current.next; 
		}
	}

链表元素修改

这个很简单,我们的思路就是找它然后改变它。

public void modify() 
	{
		if(head == null)//防止链表为空
			throw new IllegalArgumentException("您还未创建链表!");
			print();
			System.out.println("请输入你想修改为的值及其位置:");
			int num,j;
			Scanner sc = new Scanner(System.in);
			num = sc.nextInt();//接收要改为值
			j = sc.nextInt();//接收修改的位置
			sc.close();
			current = head;
			for(int i=1;current != null;i++)
			{
				if(i == j)
				{
					current.val = num;//改值
					System.out.println("修改后:");
					print();
					break;
				}
				current = current.next; 
			}
	}

完整代码


注:链表类中需要使用import语句导入如下包,否则键盘输入将无法使用

import java.util.Scanner;

public class MainFunction {

	public static void main(String[] args) {
		LinkList list = new LinkList();
		for(int i = 0;i<10;i++)
		{
			list.createList(i);//链表初始化
		}
		list.add();
		list.modify();
		list.delete();
		list.insert();
	}
}



public class LinkList {
	class LinkNode//链表定义
	{
		public int val;
		LinkNode next = null;
		
		public LinkNode(int val)//对值域进行传值
			{
				this.val=val;
			}
	}
	
	LinkNode head = null;
	LinkNode current = null;
	
	
	
	public void createList(int val)//链表的创建
	{
		if(head == null)
		{
			head = new LinkNode(val);
			current = head;
		}
		else
		{
			LinkNode node = new LinkNode(val);
			current.next = node;
			current = current.next;
		}
	}
	
	public void modify() 
	{
		if(head == null)
			throw new IllegalArgumentException("您还未创建链表!");
			print();
			System.out.println("请输入你想修改为的值及其位置:");
			int num,j;
			Scanner sc = new Scanner(System.in);
			num = sc.nextInt();
			j = sc.nextInt();
			sc.close();
			current = head;
			for(int i=1;current != null;i++)
			{
				if(i == j)
				{
					current.val = num;
					System.out.println("修改后:");
					print();
					break;
				}
				current = current.next; 
			}
	}
	
	public void add()//添加函数
	{
		if(head == null)
		throw new IllegalArgumentException("您还未创建链表!");
		print();
		System.out.println("请输入你想添加的元素:");
		int num;
		Scanner sc = new Scanner(System.in);
		num = sc.nextInt();
		sc.close();
		current = head;
		while(current.next != null)//得到尾节点
		{
			current = current.next;
		}
		LinkNode node = new LinkNode(num);
		current.next = node;
		System.out.println("添加后:");
		print();
	}
	
	public void insert()//插入函数
	{
		if(head == null)
		throw new IllegalArgumentException("您还未创建链表!");
		print();
		System.out.println("请输入你想插入的元素和位置(PS:插入功能指插入到某个元素之前):");
		int num,j;
		Scanner sc = new Scanner(System.in);
		num = sc.nextInt();
		j = sc.nextInt();
		sc.close();
		current = head;
		LinkNode previousNode = null;
		for(int i=1;current != null;i++)
		{
			if(j == 1)
			{
				LinkNode node = new LinkNode(num);
				node.next = head;
				head = node;
				System.out.println("插入后:");
				print();
				break;
			}
			if(i == j)
			{
				LinkNode node = new LinkNode(num);
				previousNode.next = node;
				node.next = current;
				System.out.println("添加后:");
				print();
				break;
			}
			previousNode = current;
			current = current.next; 
		}
	}
	
	public void delete()//插入函数
	{
		if(head == null)
		throw new IllegalArgumentException("您还未创建链表!");
		print();
		System.out.println("请输入你想删除的元素的位置:");
		int j;
		Scanner sc = new Scanner(System.in);
		j = sc.nextInt();
		sc.close();
		current = head;
		LinkNode previousNode = null;
		for(int i=1;current != null;i++)
		{
			if(j == 1)
			{
				head = head.next;
				System.out.println("删除后:");
				print();
				break;
			}
			if(i == j)
			{
				previousNode.next = current.next;
				System.out.println("删除后:");
				print();
				break;
			}
			previousNode = current;
			current = current.next; 
		}
	}
	
	
	public void print()//打印函数(遍历链表)
	{
		if(head == null)
		throw new IllegalArgumentException("您还未创建链表!");
		current = head;
		int i = 1;
		while(current != null)
		{
			System.out.println("第"+i+"号元素:"+" "+current.val);
			i++;
			current = current.next;
		}
	}

尾言

这就是Java的单链表实现了,有什么不懂的可以在评论区问我,正常情况下24小时之内能回复,大家一起进步嗷。

在这里插入图片描述

声明:本博客图片均为互联网资源

  • 16
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值