Java数据结构和算法(二)之链表

四、链表(Linked List)

  1. 链表是以节点的方式来存储,是链式存储
  2. 每个节点包含 data 域, next 域:指向下一个节点.
  3. 链表的各个节点不一定是连续存储.
  4. 链表分带头节点的链表和没有头节点的链表,根据实际的需求来确定

4.1 单链表

使用带head头的单向链表实现 –水浒英雄排行榜管理

4.1.1 不考虑排名,直接添加到最后

public class SingleLinkedListDemo{
	public static void main(String[] args){
		//测试
		//先创建节点
		HeroNode hero1 = new HeroNode(1,"宋江","及时雨");
		HeroNode hero2 = new HeroNode(2,"卢俊义","玉麒麟");
		HeroNode hero3 = new HeroNode(3,"吴用","智多星");
		HeroNode hero4 = new HeroNode(4,"林冲","豹子头");
		//创建链表
		SingleLinkedList singleLinkedList = new SingleLinkedList();
		//加入节点
		singleLinkedList.add(hero1);
		singleLinkedList.add(hero2);
		singleLinkedList.add(hero3);
		singleLinkedList.add(hero4);
		//显示
		singleLinkedList.list();
	}
}
//定义SingleLinkedList 管理英雄
class SingleLinkedList{
	//先初始化一个头结点,头结点不要动
	private HeroNode head = new HeroNode(0,"","");

	//添加节点(直接添加到最后)
	public void add(HeroNode heroNode){
		//因为head不能动,因此我们需要一个辅助节点temp
		HeroNode temp = head;
		//遍历链表,找到最后一个节点
		while(true){
			if(temp.next == null){
				break;
			}
			//如果没有找到最后,将temp后移
			temp = temp.next;
		}
		//当退出while循环时,temp就指向了链表的最后
		//将最后这个节点的next指向新的节点
		temp.next = heroNode;
	}

	//显示链表(遍历)
	public void list(){
		//先判断链表是否为空
		if(head.next==null){
			System.out.println("链表为空!");
			return;
		}
		HeroNode temp = head.next;
		while(true){
			if(temp == null){
				brteak;
			}
			System.out.println(temp);
			//将temp后移
			temp = temp.next;
		}
	}
}

//节点类
class HeroNode{
	public int no;
	public String name;
	public String nickname;
	public HeroNode next;//指向下一个节点

	//构造器
	public HeroNode(int no,String name,String nickname){
		this.no = no;
		this.name = name;
		this.nickname = nickname;
	}

	//为了显示方法,重新toString
	@Override
	public String toString(){
		return "HeroNode[no="+no+",name="+name+",nickname="+nickname+"]";
	}
}

4.1.2 考虑排名添加英雄

在添加英雄时,根据排名将英雄插入到指定位置(如果有这个排名,则添加失败,并给出提示)

public void addByOrder(HeroNode heroNode){
	HeroNode temp = head;
	boolean flag = false;//表示添加的编号是否存在
	while(true){
		if(temp.next == null){//说明temp已经在链表的最后
			break;
		}
		if(temp.next.no > heroNode.no){//位置找到,就在temp的后面插入
			break;
		}else if(temp.next.no == heroNode.no){//说明希望添加的heroNode的编号已然存在
			flag = true;//说明编号已存在
			break;
		}
		temp = temp.next;//后移,遍历当前链表
	}
	//判断flag
	if(flag){
		System.out.printf("准备插入的英雄编号%d已经存在,不能加入\n",heroNode.no);
	}else{
		//插入到链表中,temp的后面
		heroNode.next = temp.next;
		temp.next = heroNode;
	}
}

4.1.3 单向链表的修改

修改节点的信息,根据no编号来修改,即no编号不能改

public void update(HeroNode newHeroNode){
	if(head.next == null){
		System.out.println("链表为空~");
		return;
	}
	HeroNode temp = head.next;
	boolean flag = false;//表示d                                                                                                             是否找到该节点
	while(true){
		if(temp == null){
			break;//已经遍历完链表
		}
		if(temp.no == newHeroNode.no){
			//找到
			falg = true;
			break;
		}
		temp = temp.next;
	}
	if(flag){
		temp.name = newHeroNode.name;
		temp.nickname = newHeroNode.nickname;
	}else{
		System.out.printf("没有找到编号%d的节点,不能修改\n",newHeroNode.no);
	}
}

4.1.4 单向链表的删除

public void delete(int no){
	HeroNode temp = head;
	boolean flag = false;
	while(true){
		if(temp.next == null){
			break;
		}
		if(temp.next.no == no){
			flag = true;
			break;
		}
		temp = temp.next;
	}
	if(flag){
		temp.next = temp.next.next;
	}else{
		System.out.println("要删除的%d节点不存在",no);
	}
}

注:没有被指向的节点会被垃圾回收机制回收。

4.1.5 单链表面试题

  1. 求单链表中有效节点的个数
public static int getLength(HeroNode head){
	if(head.next == null){
		return 0;
	}
	int length = 0;
	HeroNode cur = head.next;
	while(cur != null){
		length++;
		cur = cur.next;
	}
	return length;
}
  1. 查找单链表中的倒数第k个结点 【新浪面试题】
    2.1 定义方法,接收head节点和index

2.2 index表示倒数第index个节点

2.3 先把链表从头到尾遍历,得到链表的总长度getLength

2.4 得到size后,从链表的第一个开始遍历(size-index)个,就可以得到

2.5 如果找到,则返回节点,否则返回null

public static HeroNode findLastIndexNode(HeroNode head,int index){
	if(head.next == null){
		return null;
	}
	int size = getLength(head);
	//先校验index
	if(index <= 0 || index > size){
		return null;
	}
	HeroNode cur = head.next;
	for (int i=0;i<size-index;i++){
		cur = cur.next;
	}
	return cur;
}
  1. 单链表的反转【腾讯面试题,有点难度】
    3.1 先定义一个节点reserseHead = new HeroNode();

3.2 从头到尾遍历原来的链表,每遍历一个节点,就将其取出,并放在新的链表的最前端;

3.3 原来的链表的head.next = recerseHead.next

public static void reverseList(HeroNode head){
	if(head.next == null || head.next.next == null){
		return;
	}
	HeroNode cur = head.next;
	HeroNode next = null;//指向当前节点(cur)的下一个节点
	HeroNode reverseHead = new HeroNode(0,"","");
	while(cur != null){
		next = cur.next;
		cur.next = reverseHead.next;
		reverseHead.next = cur;
		cur = next;
	}
	head.next = reverseHead.next;
}
  1. 从尾到头打印单链表 【百度,要求方式1:反向遍历 。 方式2:Stack栈】
    4.1 方式1:先将单链表进行反转,然后遍历即可。这样操作会破坏原来的单链表的结构,不建议

4.2 方式2:利用栈这个数据结构,将各个节点压入到栈中,然后利用栈的先进后出特点,实现逆序打印效果。

public static void reversePrint(HeroNode head){
	if(head.next == null){
		return;
	}
	Stack<HeroNode> stack = new Stack<HeroNode>();
	HeroNode cur = head.next;
	while(cur != null){
		stack.push(cur);
		cur = cur.next;
	}
	while(stack.size()>0){
		System.out.println(stack.pop());
	}
}
  1. 合并两个有序的单链表,合并之后的链表依然有序
public static void mergeList(HeroNode head1,HeroNode head2,HeroNode newHead){
	HeroNode curNew = newHead;
	HeroNode cur1 = head1.next;
	HeroNode cur2 = head2.next;
	
	if(head1.next==null && head2.next==null){
		System.out.println("两个要合并的链表都为空,无法合并");
		return;
	}

	while(cur1 != null && cur2 != null){
		if(cur1.no <= cur2.no){
			curNew.next = cur1;
			curNew = curNew.next;
			cur1 = cur1.next;
		}else{
			curNew.next = cur2;
			curNew = curNew.next;
			cur2 = cur2.next;
		}
	}

	if(cur1 == null){
		while(cur2!=null){
			curNew.next = cur2;
			curNew = curNew.next;
			cur2 = cur2.next;
		}
	}else{
		while(cur1!=null){
			curNew.next = cur1;
			curNew = curNew.next;
			cur1 = cur1.next;
		}
	}
}

4.2 双向链表

  • 单向链表,查找的方向只能是一个方向,而双向链表可以向前或者向后查找。
  • 单向链表不能自我删除,需要靠辅助节点 ,而双向链表,则可以自我删除,所以前面我们单链表删除时节点,总是找到temp,temp是待删除节点的前一个节点
public void del(int no) {
	if (head.next == null) {
		System.out.println("链表空");
		return;
	}
	HeroNode2 temp = head.next;
	boolean flag = false; // 标志变量用于确定是否有要删除的节点
	while (true) {
		if (temp == null) {
			break;
		}
		if (temp.no == no) {//找到了
			flag = true;
			break;
		}
		temp = temp.next; // temp后移
	}
	if (flag) { // 可以删除
		// temp.next = temp.next.next
		temp.pre.next = temp.next;
		if (temp.next != null) {
			temp.next.pre = temp.pre;****
		}
	} else {
		System.out.printf("要删除的no=%d 不存在\n", no);
	}
}

4.3 单向环形链表

约瑟夫问题

class Boy{
	private int no;
	private Boy next;

	public Boy(int no){
		this.no = no;
	}

	public int getNo(){
		return no;
	}

	public void setNo(int no){
		this.no = no;
	}

	public Boy getNext(){
		return next;
	}

	public void setNext(Boy next){
		this.next = next;
	}
}
//创建一个环形的单向链表
class CircleSingleLinkedList{
	//创建一个first节点,当前没有编号
	private Boy first = null;

	//添加小孩节点,构建成一个环形的链表
	public void addBoy(int nums){
		if(nums<1){
			System.out.println("nums的值不正确");
			return;
		}
		Boy curBoy = null;//辅助指针,帮助构建环形链表
		for(int i = 1;i<=nums;i++){
			Boy boy = new Boy(i);
			if(i == 1){
				first = boy;
				first.setNext(first);
				curBoy = first;
			}else{
				curBoy.setNext(boy);
				boy.setNext(first);
				curBoy = boy;
			}
		}
	}

	//遍历当前的环形链表
	public void showBoy(){
		//判断链表是否为空
		if(first == null){
			System.out.println("没有任何小孩");
			return;
		}
		Boy curBoy = first;
		while(true){
			System.out.printf("小孩的编号%d\n",curBoy.getNo());
			if(curBoy.getNext() == first){
				break;
			}
			curBoy = curBoy.getNext();
		}
	}

	//根据用户的输入,计算出小孩出圈的顺序
	//startNo 从第几个小孩开始数
	//countNum 数几下
	//nums 最初小孩数
	public void countBoy(int startNo,int countNum,int nums){
		if(first == null || startNo<1 || startNo>nums){
			System.out.println("参数输入有误,请重新输入");
			return;
		}
		Boy helper = first;
		while(true){
			if(helper.getNext() == first){//说明helper指向最后小孩节点
				break;
			}
			helper = helper.getNext();
		}
		//小孩报数前,先让first和helper移动k-1次
		for(int j=0;j<startNo-1;j++){
			first = first.getNext();
			helper = helper.getNext();
		}
		//当小孩报数时,让first和helper指针同时移动m-1次,然后出圈
		//这里是一个循环操作,直到圈中只有一个节点
		while(true){
			if(helper == first){//说明圈中只有一个节点
				break;
			}
			//让first和helper指针同时移动countNum-1
			for(int j=0;j<countNum-1;j++){
				first = first.getNext();
				helper = helper.getNext();
			}
			//这时first指向的节点,就是要出圈的小孩节点
			System.out.printf("小孩%d出圈\n",first.getNo());
			//这时将first指向的小孩节点出圈
			first = first.getNext();
			helper.setNext(first);
		}
		System.out.printf("最后留在圈中的小孩编号%d\n",first.getNo());
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值