数据结构——单向链表
本文最后更新于 198 天前,其中的信息可能已经有所发展或是发生改变。

1.文件注释内容

/**
 * @filename:    linklist.c
 * @brief:       linklist
 * @author:      kun
 * @date:        2026/2/1
 * @version:     ver1.0
 * @note         none
 * CopyRight (c) 2026   name  All Right Reseverd
 */

2.头文件

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

3.定义管理结构体和初始化链表

typedef struct linklist {
    int data;
    struct linklist *next;
} lklist_t;

// 统一分配内存的逻辑,减少重复代码
lklist_t* create_node(int value) {
    lklist_t *node = (lklist_t *)calloc(1, sizeof(lklist_t));
    if (node) {
        node->data = value;
    }
    return node;
}

// 初始化
lklist_t* linklist_init() {
    return create_node(0); // 头节点 data 域通常不使用
}

4.头插法

//头插法
bool linklist_headaddvalue(lklist_t *link, int value) {
    lklist_t *node = create_node(value);
    if (!node) return false;

    node->next = link->next;
    link->next = node;
    return true;
}

5.尾插法

//尾插法
bool linklist_lastaddvalue(lklist_t *link, int value) {
    lklist_t *node = create_node(value);
    if (!node) return false;

    lklist_t *curr = link;
    while (curr->next != NULL) {
        curr = curr->next;
    }
    curr->next = node;
    return true;
}

6.中间插法

//中间插入
bool linklist_middleaddvalue(lklist_t *link, int value, int dest) {
    lklist_t *curr = link->next; // 从第一个有效节点开始找
    while (curr != NULL) {
        if (curr->data == dest) {
            lklist_t *node = create_node(value);
            if (!node) return false;

            node->next = curr->next;
            curr->next = node;
            return true;
        }
        curr = curr->next;
    }
    return false; // 没找到目标点
}

7.头删法

//头删法
bool headdel(lklist_t* link)
{   
    //链表为空
    if(!link->next)
    {
        printf("链表为空,删除失败!\n");
        return false;
    }
    //链表非空
    lklist_t* firstnode = link->next;
    link->next = link->next->next;
    firstnode->next = NULL;
    free(firstnode);
    return true;
}

8.尾删法

//尾删法
bool lastdel(lklist_t* link)
{
    //链表为空
    if(!link->next)
    {
        printf("链表为空,删除失败!\n");
        return false;
    }
    //链表非空
    lklist_t *curr = link;
    lklist_t *lastprev;
    while (curr->next) {
        lastprev = curr;
        curr = curr->next;
    }
    lastprev->next = NULL;
    curr->next = NULL;
    free(curr);
    return true;
}

9.中间删

//中间删
bool middledel(lklist_t* link,int dest)
{
    //链表为空
    if(!link->next)
    {
        printf("链表为空,删除失败!\n");
        return false;
    }
    //链表非空
    lklist_t *curr = link;
    lklist_t *destprev;
    while (curr->next) {
        destprev = curr;
        curr = curr->next;
        if(curr->data == dest)break;
    }
    destprev->next = curr->next;
    curr->next = NULL;
    free(curr);
    return true;
}

10.内存释放

// 释放内存
void linklist_destroy(lklist_t *link) {
    lklist_t *curr = link;
    while (curr) {
        lklist_t *next = curr->next;
        free(curr);
        curr = next;
    }
}

11.链表遍历

//遍历链表
void linklist_printf(lklist_t *link) {
    lklist_t *curr = link->next;
    if (!curr) printf("链表为空,遍历失败!\n");
    while (curr) {
        printf("data = %d\n ", curr->data);
        curr = curr->next;
    }
}

12.编写main函数进行测试

int main() {
    lklist_t *list = linklist_init();
    linklist_headaddvalue(list, 10);
    linklist_headaddvalue(list, 11);
    linklist_headaddvalue(list, 12);
    linklist_headaddvalue(list, 13);
    linklist_headaddvalue(list, 14);
    middledel(list,14);
    linklist_printf(list); 
    linklist_destroy(list);
    return 0;
    /*data = 13
      data = 12
      data = 11
      data = 10*/
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇