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;
}
相关推荐
禾叙_10 分钟前
【netty】Channel
开发语言·javascript·ecmascript
云深处@16 分钟前
【C++11】包装器,智能指针
开发语言·c++
量子炒饭大师23 分钟前
【C++入门】Cyber深度漫游者的初始链路——【类与对象】初始化成员列表
开发语言·c++·dubbo·类与对象·初始化成员列表
独自破碎E28 分钟前
BISHI43 讨厌鬼进货
android·java·开发语言
纯.Pure_Jin(g)41 分钟前
【Python练习四】Python 算法与进阶特性实战:数组、序列化与位运算专项练习(3道经典练习带你巩固基础——看完包会)
开发语言·vscode·python
阿猿收手吧!44 分钟前
【C++】模块:告别头文件新时代
开发语言·c++
星火开发设计1 小时前
虚析构函数:解决子类对象的内存泄漏
java·开发语言·前端·c++·学习·算法·知识
t198751281 小时前
MATLAB水声信道建模:方法、实现与应用
开发语言·matlab
maplewen.1 小时前
C++ 多态原理深入理解
开发语言·c++·面试
龙山云仓1 小时前
No152:AI中国故事-对话祖冲之——圆周率与AI精度:数学直觉与极限探索
大数据·开发语言·人工智能·python·机器学习