千峰Java教程:073. 数据结构之链表③

书接上文

接下来还有一个功能,插入。

public class Demo1
{
	public static void main(String[] args)
	{
		NodeManager nm = new NodeManager();
		nm.add(5);
		nm.add(4);
		nm.add(3);
		nm.add(2);
		nm.add(1);
		nm.print();
		nm.del(3);
		nm.print();
		System.out.println(nm.find(1));
		System.out.println(nm.find(3));
		nm.update(2, 6);
		nm.print();
		nm.insert(1, 10);
		nm.print();
	}
}

class NodeManager
{
	private Node root;
	private int currentIndex = 0;//节点的序号,每次操作从0开始,这里演示的是后插法,在其中一个数据的后面插入
	
	public void add(int data)
	{
		if(root == null)
		{
			root = new Node(data);
		}
		else
		{
			root.addNode(data);
		}
	}
	public void del(int data)
	{
		if(root == null)
		{
			System.out.println("链表中没有数据!");
			return;
		}
		if(root.getData() == data)
		{
			root = root.next;
		}
		else
		{
			root.delNode(data);
		}
	}
	public void print()
	{
		if(root != null)
		{
			System.out.print(root.getData() + "->");
			root.printNode();
			System.out.println();
		}
	}
	public boolean find(int data)
	{//147
		if(root == null)
			return false;
		if(root.getData() == data)
		{
			return true;
		}
		else
		{
			return root.findNode(data);
		}
	}
	public boolean update(int oldData, int newData)
	{//162
		if(root == null)
		{
			System.out.println("链表中找不到数据!");
			return false;
		}
		if(root.getData() == oldData)
		{
			root.setData(newData);
			System.out.println("替换成功!");
			return true;
		}
		else
		{
			return root.updateNode(oldData,newData);
		}
	}
	public void insert(int index, int data)//注意,这里面传递的第一个是所在的索引,不是data(数)字,第二个是data
	{//197
		if(index < 0)//插入的数据不可能在第一个的前面吧
		{
			return;
		}
		currentIndex = 0;//上面说了,每次运行这个方法,都要从头开始往后数,一开始等于0
		if(index == currentIndex)//这个是要插入到第一个的后面
		{
			Node newNode = new Node(data);//这里把data放进去
			newNode.next = root.next;//这里跟下一行我在博客里放图来解释
			root.next = newNode;
		}
		else
		{
			root.insertNode(index, data);//这样就进入内部类(递归)来看要添加到哪里
		}
	}
	
	private class Node
	{
		private int data;
		private Node next;
		
		public Node(int data)
		{
			this.data = data;
		}
		public void setData(int data)
		{
			this.data = data;
		}
		public int getData()
		{
			return data;
		}
		
		//下面是增、删、输出、查询、修改、插入方法。这些方法只能是NodeManager来用,主函数不知道这些,这时候,就在NodeManager里也加上这些方法
		public void addNode(int data)
		{
			if(this.next == null)
			{
				this.next = new Node(data);
			}
			else
			{
				this.next.addNode(data);
			}
		}
		public void delNode(int data)
		{
			if(this.next != null)
			{
				if(this.next.data == data)
				{
					this.next = this.next.next;
				}
				else
				{
					this.next.delNode(data);
				}
			}
		}
		public void printNode()
		{
			if(this.next != null)
			{
				System.out.print(this.next.data + "->");
				this.next.printNode();
			}
		}
		public boolean findNode(int data)
		{//59
			if(this.next != null)
			{
				if(this.next.data == data)
				{
					return true;
				}
				else
				{
					return this.next.findNode(data);
				}
			}
			return false;
		}
		public boolean updateNode(int oldData, int newData)
		{//72
			if(this.next == null)//递归过来,如果这个节点是空,则返回false
			{
				return false;
			}
			else//否则
			{
				if(this.next.data == oldData)//如果这个节点是咱们要找的这个节点,就更新
				{
					this.next.data = newData;
					return true;
				}
				else//否则接着往下递归
				{
					return this.next.updateNode(oldData, newData);
				}
			}
		}
		public void insertNode(int index, int data)
		{//92
			currentIndex++;//这个时候本身节点++,看看是不是跟传进类的节点一样
			if(index == currentIndex)//一样的话就加在这个节点的后面
			{
				Node newNode = new Node(data);//注意这里也要放data
				newNode.next = this.next;
				this.next = newNode;
			}
			else//如果不是这个节点,递归往下看下一个节点
			{
				this.next.insertNode(index, data);
			}
		}
	}
}
/*
结果:
5->4->3->2->1->
5->4->2->1->
true
false
5->4->6->1->
5->10->4->6->1->
*/
//可以看到,在第一个data(5)之后,又插入了一个data(10)

后插法,我们来看一看如何添加:

如果我们想在第一个链子的后面添加一个链子,像下面的图这样,在第一个跟第二个链子中间加一个链子,我们首先要让下面的链子指向第二个链子,然后再把第一个链子指向第二个链子的指针改成第一个链子指向下面的链子,大功告成。另外,我们可以加一个所谓的头节点,设置data==null,这样,就可以在等于null的data后面增加一个节点了(节点就是链子)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值