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

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

#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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值