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 概述

在单向链表中删除节点,首先要根据预先确定的节点标志,判断待删除节点是否存在于链表之中,如果链表中不存在该节点,则无需进行删除操作。如果该节点存在于链表之中,在删除该节点时,还需要区分以下两种情况:

  • 待删除节点是链表的首节点;
  • 待删除节点不是链表的首节点(即该节点是中间节点或末尾节点)。

下面通过伪代码的形式介绍在单向链表中删除节点的算法。

算法:DeleteLinkedlist(list,target)
目的:在单向链表中删除节点
前提:链表和要删除的节点信息
后续:无
返回:新链表
{
    if (list == null)           // empty list
    {
        // nothing to do
        return
    }
    
    cur <- list
    
    while (((*cur).num != (*target).num) && ((*cur).link != null))    // find target node location
    {
        pre <- cur
        cur <- (*cur).link
    }
    
    if ((*cur).num == (*target).num)    // target node in the list
    {
        if (list == cur)                // target node is the first node
        {
            list <- (*cur).link
        }
        else                            // target node is not the first node
        {
            (*pre).link <- (*cur).link
        }
    }
    else
    {
        // target node not found
    }
    
    return list
}

2 代码示例

根据上述内容,可以编写在单向链表中删除节点的代码示例。

代码示例内容如下:

#include <stdio.h>

#define STRUCT_LEN sizeof(struct student)

struct student
{
    int stu_num;                /* student number */
    float stu_score;            /* student score */
    struct student* next;
};

int main()
{
    /* declaration of func */
    struct student * delete(struct student * head, struct student * target_node);
    void print(struct student * list);

    /* create a static linked list */
    struct student * list;
    struct student stu_1, stu_2, stu_3;
    stu_1.stu_num = 1;
    stu_1.stu_score = 88;
    stu_2.stu_num = 2;
    stu_2.stu_score = 66;
    stu_3.stu_num = 3;
    stu_3.stu_score = 100;
    list = &stu_1;
    stu_1.next = &stu_2;
    stu_2.next = &stu_3;
    stu_3.next = NULL;

    /* print linked list before deletion */
    printf("before deletion, ");
    print(list);

    /* create target node that to be deleted from linked list */
    struct student stu_del;
    stu_del.stu_num = 3;
    stu_del.stu_score = 0;         /* any value, because this item not compare */
    
    /* delete target node from linked list */
    list = delete(list, &stu_del);

    /* print linked list after deletion */
    printf("after deletion, ");
    print(list);

    return 0;
}

/*
** this is the delete linked list function.
*/
struct student * delete(struct student * head, struct student * target_node)
{
    struct student * cur;
    struct student * pre;
    struct student * target;

    cur = head;                 /* let cur point first node */
    target = target_node;       /* let target point the node that to be deleted */

    if (NULL == head)           /* empty list */
    {
        printf("list is null!\n");
        return head;
    }

    while (((*target).stu_num != (*cur).stu_num) && ((*cur).next != NULL))  /* find target node location */
    {
        pre = cur;
        cur = (*cur).next;
    }

    if ((*target).stu_num == (*cur).stu_num)        /* target node in the list */
    {
            if (head == cur)                        /* target node is the first node */
            {
                head = (*cur).next;
            }
            else                                    /* target node is not the first node */
            {
                (*pre).next = (*cur).next;
            }
    }
    else
    {
        printf("\ntarget node not found!\n\n");
    }

    return head;
}

/*
*  this is the print linked list content function.
*/
void print(struct student * list)
{
    struct student *walker;
    walker = list;

    printf("the linked list contents(student number and score) as followed:\n");
    printf("[student number] [student score]\n");

    while (walker != NULL)
    {
        printf("%d                %-f\n", (*walker).stu_num, (*walker).stu_score);
        walker = (*walker).next;
    }
    
    return;
}

上述代码的编译及运行结果如下:

说明:

  • 上述代码中使用了待删除节点的学生学号信息“std_num”作为删除标志,除此之外的待删除节点的其他信息,不影响节点删除逻辑;
  • 为了验证在待删除节点是链表首节点、链表中间节点和链表末尾节点的情况,可以通过修改待删除节点的结构体成员“stu_del.stu_num”的值,进行对应的测试;(上面已给出了这几种场景对应的测试结果)
  • 为了简化代码内容,上述代码示例使用的是静态链表,即链表的所有节点都是在程序中定义的,节点的存储空间不是临时开辟的。

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-13 17:44:00  更:2021-07-13 17:47:15 
 
开发: 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/20 18:32:34-

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