IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 数据结构和算法:打卡第六天 -> 正文阅读

[数据结构与算法]数据结构和算法:打卡第六天

单链表表面试题:

1、题目:

? ? ? ? 1)求单链表中有效节点个数

????????2)查找单链表中倒数第K个节点

????????3)单链表的反转

? ? ? ? 4)逆序打印单链表

????????5)打卡第三天与第六天总代码

2、代码实现:

????????1)求单链表中有效节点个数

public static int getLength(HeroNode head){
		if(head.next == null){
			return 0;
		}
		int length = 0;
		HeroNode temp = head.next;
		while(temp != null){
			length++;
			temp = temp.next;
		}
		return length;
	}

? ? ? ? 2)查找单链表中倒数第K个节点

public static HeroNode findLastIndexNode(HeroNode head, int index){
		if(head.next == null){
			return null;
		}
		int size = getLength(head);
		if(index <=0 || index > size){
			return null;
		}
		HeroNode temp = head.next;
		for(int i = 0; i < size - index; i++){
			temp = temp.next;
		}
		return temp;
	}

? ? ? ? 3)单链表的反转

public void reversetList(HeroNode head){
		if(head.next == null || head.next.next == null){
			return;
		}
		HeroNode temp = head.next;
		HeroNode next = null;
		HeroNode reverseHead = new HeroNode(0, "", "");
		while(temp != null){
			next = temp.next;
			temp.next = reverseHead.next;
			reverseHead.next = temp;
			temp = next;
		}
		head.next = reverseHead.next;
	}

????????4)逆序打印单链表

public static void reversePrint(HeroNode head){
		if(head.next == null){
			return;
		}
		Stack<HeroNode> stack = new Stack<HeroNode>();
		HeroNode temp = head.next;
		while(temp != null){
			stack.push(temp);
			temp = temp.next;
		}
		while(stack.size() > 0){
			System.out.println(stack.pop());
		}
	}

? ? ? ? 5)打卡第三天与第六天总代码

package linkedList;

import java.util.Stack;

public class SingleLinkedListDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		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.addList(hero1);
//		singleLinkedList.addList(hero3);
//		singleLinkedList.addList(hero4);
//		singleLinkedList.addList(hero2);
//		singleLinkedList.showList();
		singleLinkedList.addByOrder(hero1);
		singleLinkedList.addByOrder(hero3);
		singleLinkedList.addByOrder(hero4);
		singleLinkedList.addByOrder(hero2);
		singleLinkedList.showList();
		HeroNode newHeroNode = new HeroNode(2, "小卢" , "玉麒麟~");
		singleLinkedList.update(newHeroNode);
		System.out.println("修改后:");
		singleLinkedList.showList();
		singleLinkedList.delList(4);
		System.out.println("删除后:");
		singleLinkedList.showList();
		System.out.println("有效节点个数为:" + getLength(singleLinkedList.getHead())); 
		System.out.println("倒数第"+"index"+"d个节点是:" + findLastIndexNode(singleLinkedList.getHead(), 1));
		System.out.println("反转单链表:");
		singleLinkedList.reversetList(singleLinkedList.getHead());
		singleLinkedList.showList();
		System.out.println("逆序打印(没有改变链表结构):");
		reversePrint(singleLinkedList.getHead());
		
	}
	public static void reversePrint(HeroNode head){
		if(head.next == null){
			return;
		}
		Stack<HeroNode> stack = new Stack<HeroNode>();
		HeroNode temp = head.next;
		while(temp != null){
			stack.push(temp);
			temp = temp.next;
		}
		while(stack.size() > 0){
			System.out.println(stack.pop());
		}
	}
	public static HeroNode findLastIndexNode(HeroNode head, int index){
		if(head.next == null){
			return null;
		}
		int size = getLength(head);
		if(index <=0 || index > size){
			return null;
		}
		HeroNode temp = head.next;
		for(int i = 0; i < size - index; i++){
			temp = temp.next;
		}
		return temp;
	}
	public static int getLength(HeroNode head){
		if(head.next == null){
			return 0;
		}
		int length = 0;
		HeroNode temp = head.next;
		while(temp != null){
			length++;
			temp = temp.next;
		}
		return length;
	}
}
class SingleLinkedList {
	HeroNode head = new HeroNode(0, "", "");
	public HeroNode getHead(){
		return head;
	}
	public void addList(HeroNode heroNode){
		HeroNode temp = head;
		while(true){
			if(temp.next == null){
				break; 
			}
			temp = temp.next;
		}
		temp.next = heroNode;
	}
	//不考虑编号顺序
	public void showList(){
		if(head.next == null){
			System.out.println("链表为空");
				return;
			}
			HeroNode temp = head.next;
			while(true){
				if(temp == null){
					break;
				}
				System.out.println(temp);
				temp = temp.next;
			}
		}
	//考虑标号顺序
	public void addByOrder(HeroNode heroNode){
		HeroNode temp = head;
		boolean flag = false;
		while(true){
			if(temp.next == null){
				break;
			}
			if(temp.next.no > heroNode.no){
				break;
			}else if(temp.next.no == heroNode.no){
				flag = true;
				break;
			}
			temp = temp.next;
		}
		if(flag){
			System.out.printf("准备插入的英雄编号%d已经存在了,不能加入了。\n", heroNode.no);
		}else{
			heroNode.next = temp.next;
			temp.next = heroNode;
		}
	}
	public void update(HeroNode newHeroNode){
		if(head.next == null){
			System.out.println("链表为空");
			return;
		}
		HeroNode temp = head.next;
		boolean flag = false;
		while(true){
			if(temp == null){
				break;
			}
			if(temp.no == newHeroNode.no){
				flag = true;
				break;
			}
			temp = temp.next;
		}
		if(flag){
			temp.name = newHeroNode.name;
			temp.nickName = newHeroNode.nickName;
		}else{
			System.out.printf("没有找到编号%d的节点,不能修改。\n", newHeroNode.no);
		}
	}
	public void delList(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.printf("要删除的%d节点不存在\n", no);
		}
	}
	public void reversetList(HeroNode head){
		if(head.next == null || head.next.next == null){
			return;
		}
		HeroNode temp = head.next;
		HeroNode next = null;
		HeroNode reverseHead = new HeroNode(0, "", "");
		while(temp != null){
			next = temp.next;
			temp.next = reverseHead.next;
			reverseHead.next = temp;
			temp = next;
		}
		head.next = reverseHead.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;
	}
	public String toString(){
		return "HeroNode=["+"no = " + no + ",name = " + name + ",nickName = " + nickName + "]";
	}
	
}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-28 08:04:35  更:2021-07-28 08:04:55 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/3 17:55:37-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码