当前位置: 首页 > news >正文

链表。。。

目录

5.1 链表的结点

5.2 插入

5.3 链表长度

5.4 查找

5.5 指定位置删除

5.6 代码


5.1 链表的结点

一个结点包括:值和指向下一个结点的指针。

package com.qcby.链表;public class Node {int value;Node next;public Node(int val){value=val;}@Overridepublic String toString() {return "Node [value="+value+",next="+next+"]";}}

5.2 插入

分为尾插和头插。

尾插和头插法都分为两种情况。

5.3 链表长度

遍历数组,记录节点个数即可。

5.4 查找

遍历链表,如果某一个节点的值等于要查找的值,则查找成功;否则查找失败。

5.5 指定位置删除

首先判断删除的位置合不合法。如果不合法,则无法删除。

合法,则删除分为两种情况:第一种是删除头结点;第2种是删除其他位置。

如果是删头结点,直接让head等于head.next。如果是其他,则先遍历链表(用到pre 和 index,pre 是index的前一个结点),找到要删除的位置,令pre.next=index.next。

5.6 代码

package com.qcby.链表;public class LinkList {Node head=null;//尾插法public void insert(int value) {Node node=new Node(value);if(head==null) {head=node;return;}//找到最后Node index=head;while(index.next!=null) {index=index.next;}//插入index.next=node;}//头插法public void insertHead(int value) {Node node=new Node(value);if(head==null) {head=node;return;}node.next=head;head=node;}//链表长度public int getLen() {Node index=head;int count=0;while(index!=null) {count++;index=index.next;}return count;}//链表中查找数据public int search(int num) {Node index=head;int count=0;while(index!=null) {if(index.value==num) {return count;}else {index=index.next;}count++;}return -1;}//指定位置删除public void delete(int position) {//合法if(position<0||position>=getLen()) {System.out.println("删除位置不合法!");}//删头if(position==0) {head=head.next;}else {//先找到位置int count=0;Node index=head;Node pre=null;while(count!=position) {pre=index;index=index.next;count++;}pre.next=index.next;}}@Overridepublic String toString() {String res="[ ";Node index=head;while(index!=null) {res=res+index.value+" ";index=index.next;}res=res+"]";return res;}}
package com.qcby.链表;public class Test {public static void main(String[] args) {
//		Node node1=new Node(9);
//		Node node2=new Node(5);
//		Node node3=new Node(90);
//		Node node4=new Node(0);
//		node1.next=node2;
//		node2.next=node3;
//		node3.next=node4;
//		System.out.println(node1);LinkList list=new LinkList();list.insertHead(0);list.insertHead(10);list.insertHead(2);list.insertHead(8);list.insertHead(6);System.out.println(list);System.out.println(list.getLen());System.out.println(list.search(100));System.out.println(list.search(2));list.delete(2);System.out.println(list);}
}

http://www.dtcms.com/a/334171.html

相关文章:

  • YOLOv5、YOLOv8的损失函数、正负样本匹配策略和anchor_free/anchor_base的差异对比
  • 免费数独游戏,多难度等级挑战
  • 存储设备的核心单位:扇区、页与块
  • CentOS 7 一键部署 上Maria Database(MariaDB)10.3.38 安装手册(避开 Oracle 19c 路径)
  • 北京JAVA基础面试30天打卡11
  • JetPack系列教程(八):PDF库——让Android应用也能优雅“翻页”
  • ESP32 C3 开发板使用教程 01-测试显示屏
  • 【系统分析师】软件需求工程——第11章学习笔记(下)
  • Android 移动端 UI 设计:前端常用设计原则总结
  • 【Docker项目实战】使用Docker部署Notepad轻量级记事本
  • vscode中使用CMake Tools生成compile_commands.json文件后,如何告诉clangd这个文件在哪里呢?
  • MySQL 基础操作与编码设置:从入门到避坑
  • 范式转移:AI幻觉的终结与GPT-5的“可信”架构设计
  • 《解耦的艺术:Python 观察者模式在 GUI 与事件驱动中的实战》
  • 音视频学习(五十四):基于ffmpeg实现音频重采样
  • 【科普向-第一篇】数字钥匙生态全景:手机厂商、车厂与协议之争
  • GPFS集群性能压测
  • C++编程学习阶段性总结
  • 2025年生成式引擎优化(GEO)服务商技术能力评估报告
  • 企业运维规划及Linux介绍虚拟环境搭建
  • ROS相关的ubuntu基础教程
  • 神经网络 常见分类
  • 视觉语言模型(VLA)分类方法体系
  • 6JSON格式转python并实现数据可视化
  • RJ45 网口集成万兆(10Gbps)以太网的核心是通过物理层技术革新和信号处理优化,在传统铜缆(双绞线)介质上突破速率限制,其原理可从以下几个关键维度解析
  • Express开发快速学习
  • 探秘gRPC——gRPC原理详解
  • B3924 [GESP202312 二级] 小杨的H字矩阵
  • Flink Stream API 源码走读 - window 和 sum
  • Kubernetes Service