C语言利用函数创建链表,修改链表(插入,删除,添加),指针函数的返回

这段代码是一个简单的链表操作程序,包括创建节点、在链表末尾添加节点、在指定位置插入节点和删除指定位置的节点。以下是详细的注释:

cpp 复制代码
#include<stdio.h>
#include<stdlib.h>

// 定义链表节点结构体
struct listnode{
    int i; // 节点存储的整数值
    struct listnode *next; // 指向下一个节点的指针
}Node;

// 在链表末尾添加节点的函数声明
struct listnode *list_append(struct listnode *node,int num);

// 在指定位置插入节点的函数声明
struct listnode *list_insert(struct listnode *node,int position,int num);

// 删除指定位置节点的函数声明
struct listnode *list_delete(struct listnode *node,int position);

// 创建节点的函数声明
struct listnode* createNode(int data);

// 创建节点的函数实现
struct listnode* createNode(int data) {
    struct listnode *newNode = (struct listnode *)malloc(sizeof(struct listnode));
    newNode->i = data;
    newNode->next = NULL;
    return newNode;
}

void main(){
    // 创建一个头节点并初始化为0
    struct listnode *head = createNode(0);

    // 创建一个临时头节点用于遍历
    struct listnode *head_t;

    // 分配内存给临时头节点
    head_t = (struct listnode*)malloc(sizeof(struct listnode));

    // 打印头节点的值
    printf("%d",head->i);

    // 将临时头节点指向头节点
    head_t = head;

    // 循环创建5个节点并添加到链表中
    for(int j = 0;j<5;j++){
        struct listnode* node = (struct listnode*)malloc(sizeof(struct listnode));
        node->i = j+1;
        node->next = NULL;
        head->next = node;
        head = node;
    }

    // 打印最后一个节点的值
    printf("%d",head->i);

    // 遍历链表并打印每个节点的值
    struct listnode* curr = head_t;
    while(curr){
        printf("%d",curr->i);
        curr=curr->next;
    }

    // 在链表末尾添加一个值为7的节点,并遍历打印链表
    struct listnode* curr2 = list_append(head_t,7);
    while(curr2){
        printf("%d",curr2->i);
        curr2=curr2->next;
    }

    // 在链表的第2个位置插入一个值为2的节点,并遍历打印链表
    struct listnode* curr3 = list_insert(head_t,2,2);
    while(curr3){
        printf("%d",curr3->i);
        curr3=curr3->next;
    }

    // 删除链表的第6个位置的节点,并遍历打印链表
    struct listnode* curr4 = list_delete(head_t,6);
    while(curr4){
        printf("%d",curr4->i);
        curr4=curr4->next;
    }
}

// 在链表末尾添加节点的函数实现
struct listnode *list_append(struct listnode *node,int num){
    struct listnode *node_head;
    struct listnode *node_head2;
    node_head = (struct listnode *)malloc(sizeof(struct listnode));
    node_head2 = (struct listnode *)malloc(sizeof(struct listnode));
    node_head = node;
    node_head2 = node;
    while(node_head->next){
        node_head = node_head->next;
    }
    struct listnode *node_end;
    node_end = (struct listnode *)malloc(sizeof(struct listnode));
    node_end->i = num;
    node_end->next = NULL;
    node_head->next = node_end;
    return node;
}

// 在指定位置插入节点的函数实现
struct listnode *list_insert(struct listnode *node,int position,int num){
    struct listnode *node_head;
    node_head = (struct listnode *)malloc(sizeof(struct listnode));
    int pos_1 = 0;
    node_head = node;
    while(node_head->next){
        node_head = node_head->next;
        pos_1++;
        if(pos_1==position-1){
            break;
        }
    }
    struct listnode *newnode;
    newnode = (struct listnode *)malloc(sizeof(struct listnode));
    newnode->i=num;
    newnode->next=node_head->next;
    node_head->next = newnode;
    return node;
}

// 删除指定位置节点的函数实现
struct listnode *list_delete(struct listnode *node,int position){
    struct listnode *node_head;
    node_head = (struct listnode *)malloc(sizeof(struct listnode));
    int pos_1 = 0;
    node_head = node;
    while(node_head->next){
        node_head = node_head->next;
        pos_1++;
        if(pos_1==position){
            break;
        }
    }
    if(node->next->next)
    node_head->next = node_head->next->next;
    else{
        node_head->next = NULL;
    }
    return node;
}
相关推荐
蒋星熠几秒前
如何在Anaconda中配置你的CUDA & Pytorch & cuNN环境(2025最新教程)
开发语言·人工智能·pytorch·python·深度学习·机器学习·ai
We....1 小时前
Java分布式编程:RMI机制
java·开发语言·分布式
€8111 小时前
Java入门级教程17——利用Java SPI机制制作验证码、利用Java RMI机制实现分布式登录验证系统
java·开发语言·java spi机制·远程传输数据
2301_815357701 小时前
parameterType和@Param注解的区别
java·开发语言·数据库
tyatyatya1 小时前
MATLAB中进行视觉检测入门教程
开发语言·matlab·视觉检测
2401_845417451 小时前
set和map
java·开发语言
2301_821046522 小时前
Python的深度学习
开发语言·javascript·ecmascript
小莞尔2 小时前
【51单片机】【protues仿真】基于51单片机数控直流稳压电源系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小莞尔2 小时前
【51单片机】【protues仿真】基于51单片机密码锁系统
c语言·stm32·单片机·嵌入式硬件·51单片机
用户6120414922132 小时前
C语言做的停车场管理系统
c语言·后端·敏捷开发