Skip to content

文章标签

← 返回首页地图

C 语言单向链表基础实现模板 ​

包含节点创建、尾插、查找、打印和内存释放的 C 语言示例。

c

#include <stdio.h>// 标准输入输出库(用于printf)
#include <stdlib.h> // 标准库(用于malloc和free)

// 定义链表节点结构体
typedef struct node {
    int value;          // 节点存储的数据
    struct node *next;  // 指向下一个节点的指针(自引用指针)
} node_t;               // 类型别名简化为node_t

// 初始化链表(创建头节点)
node_t *init_list(int value) {
    node_t *node = (node_t *)malloc(sizeof(node_t));  // 动态分配节点内存
    node->value = value;  // 设置节点数据
    node->next = NULL;    // 初始化next指针为NULL(链表末尾)
    return node;          // 返回新节点指针
}

// 添加新节点到链表末尾(尾插法)
void add_to_list(node_t *node, int value) {
    node_t *new_node = (node_t *)malloc(sizeof(node_t));  // 创建新节点
    new_node->value = value;  // 设置新节点数据
    new_node->next = NULL;     // 新节点作为末尾,next置NULL

    // 遍历找到当前链表的最后一个节点
    while (node->next != NULL) {
        node = node->next;  // 指针后移直到末尾
    }
    node->next = new_node;  // 将新节点链接到链表末尾
}

// 释放链表所有节点的内存
void delete_list(node_t *head) {
    node_t *current = head;  // 从头节点开始遍历
    while (current != NULL) {
        node_t *temp = current;    // 临时保存当前节点地址
        current = current->next;   // 指针先移动到下一个节点
        free(temp);                // 释放当前节点内存
    }
    // 注意:head指针在此函数外部不会自动置NULL,调用后需手动处理
}

// 打印链表内容
void print_list(node_t *node) {
    printf("[");  // 输出起始标记
    while (node != NULL) {
        printf(" %d ", node->value);  // 打印当前节点数据
        node = node->next;             // 指针移动到下一个节点
    }
    printf("]\n");  // 输出结束标记并换行
}

/**
 * 查询链表中指定值所在的节点
 * @param head 链表头节点指针
 * @param target 要查找的目标值
 * @return 找到返回节点指针,否则返回NULL
 */
node_t *search_node(node_t *head, int target) {
    node_t *current = head;
    while (current != NULL) {
        if (current->value == target) {
            return current;  // 返回匹配的节点指针
        }
        current = current->next;
    }
    return NULL;  // 未找到返回空指针
}

int main() {
    // 创建链表头节点(数据为100)
    node_t *root = init_list(100);
    // 向链表末尾添加四个节点(值分别为99,98,97,96)
    add_to_list(root, 99);
    add_to_list(root, 98);
    add_to_list(root, 97);
    add_to_list(root, 96);
    print_list(root);
    delete_list(root);
    return 0;
}
附:串口坐标字符串解析草稿(蓝桥杯 STM32 片段)
c
// 将 Rx_String 的坐标字符串解析为结构体数组
void Uart_Proc(void)
{
    char* str;
    int count = 0;
    int i = 0;

    if (uwTick - uwTick_Uart_Set_Point < 1000) return;
    uwTick_Uart_Set_Point = uwTick;

    if (Rx_Goal_Flag == 0) return;

    strncpy(str, (char*)Rx_String, Rx_Flag);
    char *p = str + 1;  // 跳过 '('

    // 依次读取整型坐标
    while (sscanf(p, "%d", &arr[count]) == 1)
    {
        count++;
        p = strstr(p, ",");  // 查找下一个逗号
        if (p == NULL) break;
        p++;                 // 跳过逗号
    }

    sprintf((char*)Lcd_String, "%3d-%3d-%3d-%3d", arr[0], arr[1], arr[2], arr[3]);
    LCD_DisplayStringLine(Line8, Lcd_String);
    memset(Rx_String, 0x00, sizeof(Rx_String));

    HAL_UART_Transmit(&huart1, Rx_String, strlen((char*)Rx_String), 0xffff);
    while (HAL_UART_GetState(&huart1) == HAL_UART_STATE_BUSY_TX);
    memset(Rx_String, 0x00, sizeof(Rx_String));

    Rx_Flag = 0;
    Rx_Goal_Flag = 0;

    // 整理为 (x, y) 坐标点
    while (count--)
    {
        if (i % 2 == 1)
        {
            lop[i / 2].x = arr[i - 1];
            lop[i / 2].y = arr[i];
            lop[i / 2].Flag = 1;
            sprintf((char*)Lcd_String, "%d-%d-%d-%d", lop[i / 2].x, lop[i / 2].y, lop[i / 2].Flag, i);
            LCD_DisplayStringLine(Line0, Lcd_String);
        }
        i++;
    }
}

记录实践,整理思路。

慢慢读,随手记。