单向链表————遍历、查找、插入结点 (基于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");
}
posted @ 2024-06-14 08:51  Dazz_24  阅读(92)  评论(0)    收藏  举报