热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

数据结构第一节之单链表,看完即可去刷题

数据结构第一节之单链表,看完即可去刷题下面的代码讲述了从创建到应用的过程。链表中节点的结构classNode{publicintval;publicNodenext

数据结构第一节之单链表,看完即可去刷题

下面的代码讲述了从创建到应用的过程。
//链表中节点的结构
class Node {
public int val;
public Node next;
public Node() {

}
public Node(int val) {this.val = val;
}

}
//链表的创建
public class MyLinkedList {

public Node head;//表示当前链表的头 默认是null/****/
public void createLinked() {this.head = new Node(12);Node node2 = new Node(22);Node node3 = new Node(32);Node node4 = new Node(42);this.head.next = node2;node2.next = node3;node3.next = node4;
}

//打印链表
public void display() {
Node cur = this.head;
while (cur != null) {
System.out.print (cur.val +" ");
//
cur = cur.next;
}
System.out.println();
}

/*** 从指定的位置newHead* 开始进行打印* @param newHead*/
public void display(Node newHead) {Node cur = newHead;while (cur != null) {System.out.print (cur.val +" ");cur = cur.next;}System.out.println();
}public Node findLastNode() {if(this.head == null) {System.out.println("head == null");return null;}Node cur = this.head;while (cur.next != null) {cur = cur.next;}return cur;
}/*** 这个代码有瑕疵 改这个代码* @return*/
public Node findLastTwoNode() {if(this.head == null) {System.out.println("想啥呢,什么都没有!");return null;}if(this.head.next == null) {System.out.println("想啥呢,只有1个节点!");return null;}Node cur = this.head;while (cur.next.next != null) {cur = cur.next;}return cur;
}public Node findN(int n) {if(this.head &#61;&#61; null) {System.out.println("单链表为空&#xff01;");return null;}if(n <&#61; 0) {System.out.println("n太小了&#xff01;");return null;}if(n > size()) {System.out.println("n太大了");return null;}int count &#61; 1;Node cur &#61; this.head;while (count !&#61; n) {count&#43;&#43;;//1cur &#61; cur.next;}return cur;
}//得到单链表的长度
public int size(){Node cur &#61; this.head;int count &#61; 0;while (cur !&#61; null) {count&#43;&#43;;//2cur &#61; cur.next;}return count;
}//查找是否包含关键字key是否在单链表当中
public boolean contains(int key) {Node cur &#61; this.head;while (cur !&#61; null) {if(cur.val &#61;&#61; key) {return true;}cur &#61; cur.next;}return false;
}//头插法
public void addFirst(int data) {//0、首先你得有个节点Node node &#61; new Node(data);//1、判断链表是不是空的if(this.head &#61;&#61; null) {this.head &#61; node;}else {//2、插入node.next &#61; this.head;this.head &#61; node;}//0、首先你得有个节点/* Node node &#61; new Node(data);//1、判断链表是不是空的node.next &#61; this.head;this.head &#61; node;*/
}//尾插法
public void addLast(int data) {//0、newNode node &#61; new Node(data);if(this.head &#61;&#61; null) {this.head &#61; node;}else {//1、cur 找尾巴Node cur &#61; this.head;while (cur.next !&#61; null) {cur &#61; cur.next;}//2、插入cur.next &#61; node;}
}/*** 该函数是找到index-1位置的节点的引用* &#64;param index* &#64;return*/
public Node moveIndex(int index) {Node cur &#61; this.head;int count &#61; 0;while (count !&#61; index-1) {cur &#61; cur.next;count&#43;&#43;;}return cur;
}//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data) {if(index <0 || index > size()) {System.out.println("index不合法");return;}//头插法if(index &#61;&#61; 0) {addFirst(data);return;}//尾插法if(index &#61;&#61; size()) {addLast(data);return;}Node cur &#61; moveIndex(index);//cur保存的是 index-1位置的节点的引用Node node &#61; new Node(data);node.next &#61; cur.next;cur.next &#61; node;
}/*** 找到关键key的前驱&#xff0c;如果有返回前驱节点的引用* 如果没有返回null* &#64;param key* &#64;return*/
public Node searchPrev(int key) {Node cur &#61; this.head;while (cur.next !&#61; null) {if(cur.next.val &#61;&#61; key) {return cur;}cur &#61; cur.next;}return null;
}
//删除第一次出现关键字为key的节点
public void remove(int key) {if(this.head &#61;&#61; null) {return;}if(this.head.val &#61;&#61; key) {this.head &#61; this.head.next;return;}Node prev &#61; searchPrev(key);if(prev &#61;&#61; null) {System.out.println("没有这个key的前驱");}else {Node del &#61; prev.next;prev.next &#61; del.next;}
}
//删除所有值为key的节点
public void removeAllKey(int key){Node prev &#61; this.head;Node cur &#61; prev.next;while (cur !&#61; null) {if(cur.val &#61;&#61; key) {prev.next &#61; cur.next;}else {prev &#61; cur;}cur &#61; cur.next;}if(this.head.val &#61;&#61; key) {this.head &#61; this.head.next;}
}public void clear() {this.head &#61; null;
}

//翻转单链表
public Node reverseList() {
Node cur &#61; this.head;
Node prev &#61; null;
Node newHead &#61; null;
while (cur !&#61; null) {
Node curNext &#61; cur.next;
if(curNext &#61;&#61; null) {
newHead &#61; cur;
}
cur.next &#61; prev;
prev &#61; cur;
cur &#61; curNext;
}
return newHead;
}

public Node reverseList2() {Node prev &#61; null;while (head !&#61; null) {Node cur &#61; this.head;this.head &#61; head.next;cur.next &#61; prev;prev &#61; cur;}return prev;
}
public Node reverseList3() {Node cur &#61; this.head;Node prev &#61; null;while (cur !&#61; null) {Node curNext &#61; cur.next;cur.next &#61; prev;prev &#61; cur;cur &#61; curNext;}return prev;
}

//找到中间节点
public Node middleNode1() {
int len &#61; size()/2;
Node cur &#61; this.head;
int count &#61; 0;
while (count !&#61; len) {
cur &#61; cur.next;
count&#43;&#43;;
}
return cur;
}

public Node middleNode() {Node fast &#61; this.head;Node slow &#61; this.head;while (fast !&#61; null && fast.next !&#61; null) {fast &#61; fast.next.next;slow &#61; slow.next;}return slow;
}

以上就是单链表的所有代码&#xff0c;大家可根据此图来理解

在这里插入图片描述


推荐阅读
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 本文介绍了游标的使用方法,并以一个水果供应商数据库为例进行了说明。首先创建了一个名为fruits的表,包含了水果的id、供应商id、名称和价格等字段。然后使用游标查询了水果的名称和价格,并将结果输出。最后对游标进行了关闭操作。通过本文可以了解到游标在数据库操作中的应用。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 本文介绍了三种方法来实现在Win7系统中显示桌面的快捷方式,包括使用任务栏快速启动栏、运行命令和自己创建快捷方式的方法。具体操作步骤详细说明,并提供了保存图标的路径,方便以后使用。 ... [详细]
  • 深度学习中的Vision Transformer (ViT)详解
    本文详细介绍了深度学习中的Vision Transformer (ViT)方法。首先介绍了相关工作和ViT的基本原理,包括图像块嵌入、可学习的嵌入、位置嵌入和Transformer编码器等。接着讨论了ViT的张量维度变化、归纳偏置与混合架构、微调及更高分辨率等方面。最后给出了实验结果和相关代码的链接。本文的研究表明,对于CV任务,直接应用纯Transformer架构于图像块序列是可行的,无需依赖于卷积网络。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
author-avatar
智亚康-Scorpio
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有