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;
}
相关推荐
程序猿阿伟2 小时前
《C++音频降噪秘籍:让声音纯净如初》
开发语言·c++·网络协议
极客小张2 小时前
基于STM32的智能家居语音控制系统:集成LD3320、ESP8266设计流程
c语言·stm32·物联网·算法·毕业设计·课程设计·语言识别
Tech_gis2 小时前
C++ 观察者模式
开发语言·c++·观察者模式
卑微求AC2 小时前
继电器原理及应用
c语言·开发语言·51单片机·嵌入式
曳渔3 小时前
Java-数据结构-反射、枚举 |ू・ω・` )
java·开发语言·数据结构·算法
laocooon5238578863 小时前
java 模拟多人聊天室,服务器与客户机
java·开发语言
风槐啊3 小时前
六、Java 基础语法(下)
android·java·开发语言
꧁༺❀氯ྀൢ躅ྀൢ❀༻꧂3 小时前
算法与程序课程设计——观光铁路
c语言·c++·算法·课程设计·dijkstra 算法·spfa算法
网安老伯3 小时前
【2024版】最新kali linux入门及常用简单工具介绍(非常详细)零基础入门到精通,收藏这一篇就够了_kalilinux
linux·运维·服务器·开发语言·web安全·网络安全·xss
laocooon5238578863 小时前
java类的混搭,
java·开发语言