• 数据结构-链表的简单操作实现


    目录

    0.链表前序工作

    1.构建出一个链表

    2.展示链表中的所有存储数据

    3.查找关键字key是否在链表中

    4.求链表的长度

    5.头插法

    6.尾插法

    7.插入任意位置(规定第一个元素位置为0下标)

    8.删除第一次出现的值为key的关键字

    9.删除所有值为key的关键字

    10.清空所有节点


    0.链表前序工作

    在进行链表的相关操作之前,首先需要进行链表的构造。

    在一个链表节点中,有两个区域:

    一个用来存储当前节点的数据-val;

    另一个需要存储下一个节点的地址。

    1. static class Node{
    2. public int val;//存储的数据
    3. public Node next;//存储下一个节点的地址;
    4. //提供一个构造方法
    5. public Node(int val){
    6. this.val = val;
    7. }
    8. }

    定义一个head,代表当前头节点的引用

    public Node head;//代表当前链表头结点的引用

    1.构建出一个链表

    构建一个链表将构建的链表节点串起来。

    这里我们构建四个节点,

    1. public void createLink(){
    2. Node node1 = new Node(1);
    3. Node node2 = new Node(1);
    4. Node node3 = new Node(1);
    5. Node node4 = new Node(1);
    6. node1.next = node2;
    7. node2.next = node3;
    8. node3.next = node4;
    9. head = node1;
    10. }

    2.展示链表中的所有存储数据

    将存储在链表中的数据进行展示,这里定义一个节点cur,和head相同的指向。

    随后将cur向后挪动,当cur的值不为空的时候,向后挪动打印;

    当cur的值为空时,证明链表到达了末尾,已经没有元素,进而跳出while循环。

    需要注意的内容:

    (1)如果要将整个链表遍历完成 那么就要head == null;
    (2)如果是遍历到链表的尾巴。条件是head.next == null;

    1. public void display(){
    2. //如果要将整个链表遍历完成 那么就要head == null;
    3. //如果是遍历到链表的尾巴。条件是head.next == null
    4. Node cur = head;
    5. while(cur != null){
    6. System.out.print(cur.val+" ");
    7. cur = cur.next;
    8. }
    9. System.out.println();
    10. }

    3.查找关键字key是否在链表中

    在链表中搜寻是否包含关键字key,

    同样我们需要一个新的节点和头节点head指向相同的位置;

    向后一直走,循环条件是节点的内容不为null,也就是(cur!=null)

    存在结果返回true,否则返回false;

    1. public boolean contains(int key){
    2. Node cur = head;
    3. while (cur != null){
    4. if(cur.val == key){
    5. return true;
    6. }
    7. cur = cur.next;
    8. }
    9. return false;
    10. }

    4.求链表的长度

    链表的长度定义为size(),首先定义变量count等于0,同样定义一个cur节点和head指向相同位置,向后遍历,循环条件为cur != null。每循环一次,计数器数值加1,指针向后挪动一个位置。

    1. public int size(){
    2. int count = 0;
    3. Node cur = head;
    4. while(cur != null){
    5. count++;
    6. cur = cur.next;
    7. }
    8. return count;
    9. }

    5.头插法

    进行头插法,首先准备好一个新的节点node,用来存放传进来的数据data;

    node的next指向head;

    让head指向node。

    1. public void addFirst(int data){
    2. Node node = new Node(data);
    3. node.next = head;
    4. head = node;
    5. }

    6.尾插法

    首先判断是否为第一次插入,当头节点为null的时候,将head指向node节点,返回。
    如果不是第一次插入节点,同样定义一个为cur的节点指向head;

    cur向后挪,直至走到链表最后,将cur的next指向新的节点node,完成尾插操作。

    1. public void addLast(int data){
    2. Node node = new Node(data);
    3. //判断是不是第一次插入
    4. if(head == null){
    5. head = node;
    6. return;
    7. }
    8. Node cur = head;
    9. while(cur.next != null){
    10. cur = cur.next;
    11. }
    12. cur.next = node;
    13. }

    7.插入任意位置(规定第一个元素位置为0下标)

    在链表中插入任意位置,需要传入两个值-位置下标、插入的数值.

    判断要插入的位置是否合法:自己写一个方法检查下标:checkIndex

    首先,如果插入下标为0下标-头插法;插入下标为链表的长度size-尾插法;

    其他情况就是在链表的中间位置:

    需要找到链表中需要插入位置的前一个位置的下标位置:findIndexSubOne。【因为我们在这里讨论的链表为单向不带头节点的链表,必须要找到需要元素需要插入位置的前一个位置,进而进行后续操作】。

    得到插入到链表中前一个位置的下标,定义为cur;要插入的数据定义为node;

    具体两步的指向如下图所示。

    1. public void addIndex(int index,int data)
    2. throws ListIndexOutOfException{
    3. checkIndex(index);
    4. if(index == 0){
    5. addFirst(data);
    6. return;
    7. }
    8. if(index == size()){
    9. addLast(data);
    10. return;
    11. }
    12. Node cur = findIndexSubOne(index);
    13. Node node = new Node(data);
    14. node.next = cur.next;
    15. cur.next = node;
    16. }

    checkIndex

    1. private void checkIndex(int index){
    2. if(index < 0 || index > size()){
    3. throw new ListIndexOutOfException("index位置不合法!");
    4. }
    5. }

    ListIndexOutOfException

    1. public class ListIndexOutOfException extends RuntimeException{
    2. public ListIndexOutOfException() {
    3. }
    4. public ListIndexOutOfException(String message) {
    5. super(message);
    6. }
    7. }

    findIndexSubOne

    1. private Node findIndexSubOne(int index){
    2. Node cur = head;
    3. int count = 0;
    4. while (count != index -1){
    5. cur = cur.next;
    6. count++;
    7. }
    8. return cur;
    9. }

    8.删除第一次出现的值为key的关键字

    删除第一次出现的值为key的关键字

    首先当链表的表头就是我们要找的关键字:将头节点向后挪动,返回即可。

    寻找到要删除节点的前一个节点:searchPrev(key)

    1. public void remove(int key){
    2. if(head.val == key){
    3. head = head.next;
    4. return;
    5. }
    6. Node cur = searchPrev(key);//代表当前节点
    7. if(cur == null){
    8. return ;
    9. }
    10. Node del = cur.next;//代表要删除的节点
    11. cur.next = del.next;
    12. }

    searchPrev(key)

    1. private Node searchPrev(int key){
    2. if(head == null){
    3. return null;//一个节点都没有
    4. }
    5. Node cur = head;
    6. while(cur.next != null){
    7. if(cur.next.val == key){
    8. return cur;
    9. }
    10. cur = cur.next;
    11. }
    12. //代表根本没有要删除的节点
    13. return null;
    14. }

    9.删除所有值为key的关键字

    在链表中删除所有值为key的关键字

    首先判断链表中有无元素,如果头节点的指向为null的话,返回return;

    在这里定义两个指针prev指向头的位置;

    cur指向头的下一个位置。

    这里选择先不进行对头结点的判定

    1. public void removeAllKey(int key){
    2. if(head == null){
    3. return;
    4. }
    5. Node prev = head;
    6. Node cur = head.next;
    7. while (cur != null){
    8. if(cur.val == key){
    9. prev.next = cur.next;
    10. cur = cur.next;
    11. }else {
    12. prev = cur;
    13. cur = cur.next;
    14. }
    15. }
    16. if(head.val == key){
    17. head = head.next;
    18. }
    19. }

    10.清空所有节点

    1. public void clear(){
    2. head = null;
    3. }

  • 相关阅读:
    竞赛 机器学习股票大数据量化分析与预测系统 - python 竞赛
    实战PyQt5: 155-QChart图表之极坐标图表
    解决 net core 3.x 跨域问题
    蜂鸣器(51单片机)
    centos7 crash调试内核
    Java实现基于Socket的负载均衡代理服务器(含六种负载均衡算法)
    ubuntu 20.04+ORB_SLAM3 安装并行全记录(无坑版)(一)
    第四章:数据操作Ⅰ 第二节:读写CSV文件
    Thinkpad X201i笔记本电脑开机Fan Error
    【无标题】
  • 原文地址:https://blog.csdn.net/weixin_47285608/article/details/134247364