C语言创建链表的基本操作——建立、插入、删除、清空、销毁

#include <stdio.h>
#include <stdlib.h>

struct  node {
    int value;
    struct node * next;
};
typedef struct node nodes;

nodes *LinkList_Init();
void Insert_value(int value_in, nodes *P_node);
void Insert_pos_value(int value_in, int pos, nodes *p_node);
void Foreach_Linklist(nodes * p_node);
void Remove_LinkList(int pos, nodes *p_node);
void Remove_value_LinkList(int value_in, nodes *p_node);
void Clear_LinkList(nodes *p_node);
void Destory_LinkList(nodes *p_node);


int main() {
    printf("---开始创建链表---\n");
    nodes* p_head = LinkList_Init();
    printf("---链表创建完毕---\n");
    
    printf("---开始后插数据--\n");
    Insert_value(1, p_head);
    Insert_value(2, p_head);
    Insert_value(3, p_head);
    Insert_value(4, p_head);
    Insert_value(5, p_head);
    Insert_value(6, p_head);
    printf("---后插数据完毕---\n");
    Foreach_Linklist(p_head);

    printf("---开始在特定位置插入数据--\n");
    Insert_pos_value(7, 1, p_head);
    printf("---在特定位置插入数据完毕---\n");
    Foreach_Linklist(p_head);

    printf("---开始在特定位置删除数据--\n");
    Remove_LinkList(0, p_head);
    printf("---在特定位置删除数据完毕---\n");
    Foreach_Linklist(p_head);

    printf("---开始在特定数值删除数据---\n");
    Remove_value_LinkList(6, p_head);
    printf("---在特定数值删除数据完毕---\n");
    Foreach_Linklist(p_head);

    printf("---开始清空链表---\n");
    Clear_LinkList(p_head);
    printf("---清空链表完毕---\n");
    Foreach_Linklist(p_head);

    printf("---开始销毁链表---\n");
    Destory_LinkList(p_head);
    p_head = NULL;
    printf("---销毁链表完毕---\n");
    Foreach_Linklist(p_head);

    system("pause");
    return 0;
}

//链表初始化
nodes *LinkList_Init() {
    nodes *p_node = (nodes*)malloc(sizeof(nodes));
    p_node -> next = NULL;
    return p_node;
}

//往后插入数据
void Insert_value(int value_in, nodes *P_node) {
    if(P_node == NULL)
        return;
    nodes *p_current = P_node;
    nodes *new_node = (nodes*)malloc(sizeof(nodes));
    new_node -> value = value_in;
    new_node -> next = NULL;
    while(p_current -> next != NULL) {
        p_current = p_current -> next;
    }
    p_current -> next = new_node;
}

//往指定位置插入数据
void Insert_pos_value(int value_in, int pos, nodes *p_node) {
    if(p_node == NULL)
        return;
    nodes *p_current = p_node -> next;
    nodes *p_privious = p_node;
    int temp = 0;
    while(p_current != NULL){
        if(temp == pos) {
            nodes *new_node = (nodes *)malloc(sizeof(nodes));
            new_node -> value = value_in;
            p_privious -> next = new_node;
            new_node -> next = p_current;
            break;
        } else {
            p_privious = p_current;
            p_current = p_current -> next;
            temp ++;
        }
    }
}

//遍历链表
void Foreach_Linklist(nodes * p_node) {
    if(p_node == NULL)
        return;
    nodes * p_current = p_node -> next;
    while(p_current != NULL) {
        printf("%d\n", p_current -> value);
        p_current = p_current -> next;
    }
}

//删除指定位置的元素 零为起始位置
void Remove_LinkList(int pos, nodes *p_node) {
    if(p_node == NULL)
        return;
    int temp = 0;
    nodes *p_current = p_node -> next;
    nodes *p_privious = p_node;
    while(p_current != NULL) {
        if(temp == pos) {
            p_privious -> next = p_current -> next;
            free(p_current);
            p_current = NULL;
        } else {
            p_privious = p_current;
            p_current = p_current -> next;
            temp ++;
        }
    }
}

//删除特定值的元素
void Remove_value_LinkList(int value_in, nodes *p_node) {
    if(p_node == NULL)
        return;
    nodes *p_current = p_node -> next;
    nodes *p_privious = p_node;
    while(p_current != NULL) {
        if(p_current -> value == value_in) {
            p_privious -> next = p_current -> next;
            free(p_current);
            p_current = NULL;
        } else {
            p_privious = p_current;
            p_current = p_current -> next;
        }
    }
}

//清空链表
void Clear_LinkList(nodes *p_node) {
    if (p_node == NULL) {
        return;
    }
    nodes *p_current = p_node -> next;
    while(p_current != NULL) {
        nodes *p_save = p_current;
        p_current = p_current -> next;
        free(p_save);
        p_save = NULL;
    }
    p_node -> next = NULL;
}

//销毁链表
void Destory_LinkList(nodes *p_node) {
    if (p_node == NULL) {
        return;
    }
    
    Clear_LinkList(p_node);
    free(p_node);
    p_node = NULL;
}
  • 6
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值