首页 > 其他分享 >单向链表————遍历、查找、插入结点 (基于C语言实现)

单向链表————遍历、查找、插入结点 (基于C语言实现)

时间:2024-06-14 09:25:34浏览次数:8  
标签:结点 Head next 链表 LList Manager Phead C语言

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdbool.h>

// 指的是单向链表中的结点有效数据类型,用户可以根据需要进行修改
typedef int DataType_t;

// 构造链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct LinkedList
{
    DataType_t data;         // 结点的数据域
    struct LinkedList *next; // 结点的指针域

} LList_t;

// 创建一个空链表,空链表应该有一个头结点,对链表进行初始化
LList_t *LList_Create(void)
{
    // 1.创建一个头结点并对头结点申请内存
    LList_t *Head = (LList_t *)calloc(1, sizeof(LList_t));
    if (NULL == Head)
    {
        perror("Calloc memory for Head is Failed");
        exit(-1);
    }

    // 2.对头结点进行初始化,头结点是不存储有效内容的!!!
    Head->next = NULL;

    // 3.把头结点的地址返回即可
    return Head;
}

// 创建新的结点,并对新结点进行初始化(数据域 + 指针域)
LList_t *LList_NewNode(DataType_t data)
{
    // 1.创建一个新结点并对新结点申请内存
    LList_t *New = (LList_t *)calloc(1, sizeof(LList_t));
    if (NULL == New)
    {
        perror("Calloc memory for NewNode is Failed");
        return NULL;
    }

    // 2.对新结点的数据域和指针域进行初始化
    New->data = data;
    New->next = NULL;

    return New;
}

// 指定插入
bool LList_Insert(LList_t *Head, DataType_t dest, DataType_t data)
{
    LList_t *Phead = Head;
    // 1.创建新的结点,并对新结点进行初始化
    LList_t *New = LList_NewNode(data);
    if (NULL == New)
    {
        printf("can not insert new node\n");
        return false;
    } // 定义p0为需增加的新节点

    if (NULL == Head->next)
    {
        printf("链表为空\n");
        return false;
    }

    while (Phead->data != dest)
    {
        if (Phead->next == NULL)
        {
            printf("未找到指定元素\n");
            return false;
        }
        Phead = Phead->next;
    }

    New->next = Phead->next;
    Phead->next = New;

    return true;
}

// 头插
bool LList_HeadInsert(LList_t *Head, DataType_t data)
{
    // 1.创建新的结点,并对新结点进行初始化
    LList_t *New = LList_NewNode(data);
    if (NULL == New)
    {
        printf("can not insert new node\n");
        return false;
    }

    // 2.判断链表是否为空,如果为空,则直接插入即可
    if (NULL == Head->next)
    {
        Head->next = New;
        return true;
    }

    // 3.如果链表为非空,则把新结点插入到链表的头部
    New->next = Head->next;
    Head->next = New;

    return true;
}

// 尾插
bool LList_TailInsert(LList_t *Head, DataType_t data)
{
    LList_t *Phead = Head;

    // 1.创建新的结点,并对新结点进行初始化
    LList_t *New = LList_NewNode(data);
    if (NULL == New)
    {
        printf("can not insert new node\n");
        return false;
    }

    // 2.判断链表是否为空,如果为空,则直接插入即可
    if (NULL == Head->next)
    {
        Head->next = New;
        return true;
    }

    // 3.循环找到链表的末尾结点
    while (Phead->next != NULL)
    {
        Phead = Phead->next;
    }

    // 4.把新结点插入到链表的尾部
    Phead->next = New;

    return true;
}

// 指定元素删除
bool LList_DestDel(LList_t *Head, DataType_t dest)
{
    LList_t *Phead = Head;
    LList_t *Point;
    if (NULL == Head->next)
    {
        return false;
    }
    while (Phead && Phead->next->data != dest)
    { // 遍历链表找到要删除的数 或 Head为NULL
        Phead = Phead->next;
    }
    if (Phead != NULL)
    {
        Point = Phead->next;
        Phead->next = Phead->next->next;
        Point->next = NULL;
        free(Point);
        return true;
    }
    else
    {
        printf("未找到该数\n");
        return false;
    }
}
// 头删
bool LList_Headdelete(LList_t *Head)
{
    LList_t *Phead = Head->next;
    // 2.判断链表是否为空,如果为空,则直接退出即可
    if (NULL == Head->next)
    {
        printf("链表为空\n");
        return false;
    }
    Head->next = Phead->next;
    Phead->next = NULL;
    free(Phead);
}

// 尾删
bool LList_Taildelete(LList_t *Head)
{
    LList_t *Phead = Head->next;
    LList_t *Phead_prev = Head;
    // 2.判断链表是否为空,如果为空,则直接退出即可
    if (NULL == Head->next)
    {
        printf("链表为空\n");
        return false;
    }
    while (Phead->next != NULL)
    {
        Phead_prev = Phead_prev->next;
        Phead = Phead->next;
    }
    Phead_prev->next = NULL;
    free(Phead);
}

// 遍历链表
bool LList_Print(LList_t *Head)
{
    // 对单向循环链表的头结点的地址进行备份
    LList_t *Phead = Head;

    // 判断当前链表是否为空,为空则直接退出
    if (Head->next == Head)
    {
        printf("current linkeflist is empty!\n");
        return false;
    }

    // 从首结点开始遍历
    while (Phead->next)
    {
        // 把头结点的直接后继作为新的头结点
        Phead = Phead->next;

        // 输出头结点的直接后继的数据域
        printf("data = %d\n", Phead->data);

        // 判断是否到达尾结点,尾结点的next指针是指向首结点的地址
        if (Phead->next == NULL)
        {
            break;
        }
    }

    return true;
}

int main(int argc, char const *argv[])
{

    // 1.创建顺序表
    LList_t *Manager = LList_Create();

    // 2.向顺序表中的尾部插入新元素
    // printf("*********************************尾插********************************\n");
    LList_TailInsert(Manager, 5);
    LList_TailInsert(Manager, 2);
    LList_TailInsert(Manager, 1);
    LList_TailInsert(Manager, 4);
    LList_TailInsert(Manager, 6);

    // //3.遍历顺序表
    // LList_Print(Manager); // -- 5 2 1 4 6
    // printf("\n");
    //  4.向顺序表中的头部插入新元素
    //  printf("*********************************头插********************************\n");
    LList_HeadInsert(Manager, 9);
    LList_HeadInsert(Manager, 7);
    LList_HeadInsert(Manager, 8);
    LList_HeadInsert(Manager, 0);
    LList_HeadInsert(Manager, 10);

    // //5.遍历顺序表
    // LList_Print(Manager); // --10 0 8 7 9 5 2 1 4 6
    // printf("\n");
    // 6.删除顺序表的元素
    // printf("*********************************头删********************************\n");
    LList_Headdelete(Manager);
    LList_Headdelete(Manager);
    LList_Headdelete(Manager);
    LList_Headdelete(Manager);
    LList_Headdelete(Manager);

    // LList_Print(Manager); // --5  2  1  4  6

    // 7.指定插入
    // printf("*********************************指定插入********************************\n");
    LList_Insert(Manager, 5, 1);
    LList_Insert(Manager, 2, 3);
    LList_Insert(Manager, 6, 7);

    // 8.遍历顺序表
    // LList_Print(Manager); // --5 1 2 3 1 4 6 7
    // printf("\n");

    // 9.指定删除
    // printf("*********************************指定删除********************************\n");
    LList_DestDel(Manager, 5);
    LList_DestDel(Manager, 7);
    LList_DestDel(Manager, 3);

    // 10.遍历顺序表
    // LList_Print(Manager); // -- 1 2 1 4 6
    // printf("\n");

    // 11.尾部删除
    // printf("*********************************尾删********************************\n");
    LList_Taildelete(Manager);
    LList_Taildelete(Manager);
    LList_Taildelete(Manager);
    LList_Taildelete(Manager);

    // 12.遍历顺序表
    LList_Print(Manager); // --1
    // printf("\n");
}

标签:结点,Head,next,链表,LList,Manager,Phead,C语言
From: https://www.cnblogs.com/Dazz24/p/18247108

相关文章

  • C语言数据结构实现-静态链表2-基本操作
    上节,我们初步创建了一个静态链表,本节学习有关静态链表的一些基本操作,包括对表中数据元素的添加、删除、查找和更改。本节是建立在已能成功创建静态链表的基础上,因此我们继续使用上节中已建立好的静态链表学习本节内容,建立好的静态链表如图1所示:静态链表添加元素例如,在图1......
  • C语言-运算符
    运算符有几个操作数就是几元运算符1、算术运算符*运算符**描述**操作数个数(几元运算符)**组成的表达式的值**副作用*+正号1操作数本身无-负号1操作数符号取反无+加号2两个操作数之和无-减号2两个操作数之差无*****乘号2两个......
  • C语言数据结构实现-静态链表1-初始化
    《顺序表和链表优缺点》一节,我们了解了两种存储结构各自的特点,那么,是否存在一种存储结构,可以融合顺序表和链表各自的优点,从而既能快速访问元素,又能快速增加或删除数据元素。静态链表,也是线性存储结构的一种,它兼顾了顺序表和链表的优点于一身,可以看做是顺序表和链表的升级版。使......
  • 第一个c语言程序
    我们有两种方式从计算机获得信息:一是看屏幕上的文字、图片、视频等,二是听从喇叭发出来的声音。让喇叭发出声音目前还比较麻烦,我们先来看看如何在屏幕上显示一些文字吧。在屏幕上显示文字非常简单,只需要一个语句,例如,下面的代码会让屏幕显示出“C语言中文网”:puts("C语言中文网......
  • 3个月搞定计算机二级C语言!高效刷题系列进行中
    前言大家好,我是梁国庆。计算机二级应该是每一位大学生的必修课,相信很多同学的大学flag中都会有它的身影。我在大学里也不止一次的想要考计算机二级office,但由于种种原因,备考了几次都不了了之。这一次我想换个目标!备考计算机二级C语言今天山东省考试院发布了关于2024年9月全......
  • 线性表的链式表示——链表
    目录一、单链表1、单链表的定义2、单链表的基本操作 (1)单链表的初始化(2)插入操作(3)删除操作(4)查找操作(5)求表长操作(6)单链表的建立 二、双链表三、循环链表 1、循环单链表2、循环双链表四、静态链表 五、顺序表和链表的比较1、存取方式2、逻辑结构与物理结构3......
  • C语言练习题05
    练习1:使用二维数组计算杨辉三角#include<stdio.h>#include<string.h>#include<stdlib.h>intmain(intargc,constchar*argv[]){intm,i,j;printf("请输入你所要打印的杨辉三角的列数:\n");scanf("%d",&m);putchar(10);int......
  • C语言练习题04
    练习1:输入一个5个元素的一维数组,实现冒泡排序。#include<stdio.h>#include<string.h>#include<stdlib.h>intmain(intargc,constchar*argv[]){ inttemp; inta[5]={0}; for(inti=0;i<5;i++) { printf("请输入一个数:\n"); scanf("%d"......
  • c语言编译器的分类
    前言在上节《C语言编译和链接》中我们已经讲解了C语言编译器的概念,由于C语言的历史比较久,而且早期没有规范,整个计算机产业也都处于拓荒的年代,所以就涌现了很多款C语言编译器,它们各有特点,适用于不同的平台,本节就来给大家科普一下。我们分两部分介绍C语言的编译器,分别是......
  • C语言题目:排序问题2
    题目描述将十个数进行从大到小的顺序进行排列输入格式十个整数输出格式以从大到小的顺序输出这个十个数样例输入12345678910样例输出10987654321代码解析1.引入头文件代码首先引入了stdio.h头文件,这是C语言标准输入输出库,用于处理输入输出......