C语言-单向链表

/*
本程序使用mingw编译
源文件是ANSI中的GBK字符编码格式.
编译参数:
-fexec-charset=GBK
-finput-charset=GBK
*/

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

struct 结点
{
    int 数据;   //数据域
    struct 结点 *下一个;   //指针域
};

struct 结点 *创建链表();
struct 结点 *创建结点(int 数据);
void 遍历链表(struct 结点 *头结点);
struct 结点 *查找结点(struct 结点 *头结点, unsigned int  序号);
bool 删除结点(struct 结点 *头结点, unsigned int  序号);
bool 插入结点(struct 结点 *头结点, struct 结点 *被插入的结点, unsigned int  序号);
int 取链表结点数(struct 结点 *头结点);
void 追加结点(struct 结点 *头结点, struct 结点 *被追加的结点);
void 释放链表(struct 结点 *头结点);

int main(void)
{
    struct 结点 *头指针;
    头指针 = 创建链表();
    插入结点(头指针, 创建结点(123), 1);
    插入结点(头指针, 创建结点(456), 2);
    插入结点(头指针, 创建结点(789), 3);
    插入结点(头指针, 创建结点(345), 4);
    printf("第一次输出:\n");
    遍历链表(头指针);
    删除结点(头指针, 2);
    printf("第二次输出:\n");
    遍历链表(头指针);
    插入结点(头指针, 创建结点(999), 3);
    printf("第三次输出:\n");
    遍历链表(头指针);
    int 长度 = 取链表结点数(头指针);
    printf("链表长度为:%d\n", 长度);
    追加结点(头指针, 创建结点(135790));
    printf("第四次输出:\n");
    遍历链表(头指针);
    释放链表(头指针);

    return 0;
}

struct 结点 *创建链表()
{
    struct 结点 *头结点 = (struct 结点 *)malloc(sizeof(struct 结点));
    头结点->下一个 = NULL;
    return 头结点;
}

struct 结点 *创建结点(int 数据)
{
    struct 结点 *结点 = (struct 结点 *)malloc(sizeof(struct 结点));
    结点->数据 = 数据;
    结点->下一个 = NULL;
    return 结点;
}

void 遍历链表(struct 结点 *头结点)
{
    struct 结点 *临时 = 头结点->下一个;

    for(int 计数 = 1; 临时; 计数++)
    {
        printf("第%d个结点的数据域为:%d\n", 计数, 临时->数据);
        临时 = 临时->下一个;
    }
}

struct 结点 *查找结点(struct 结点 *头结点, unsigned int  序号)
{
    struct 结点 *临时 = 头结点;

    for(unsigned int 计数 = 0; 计数 < 序号 && (临时 = 临时->下一个); 计数++)
        ;
    return 临时;
}


bool 删除结点(struct 结点 *头结点, unsigned int  序号)
{
    struct 结点 *临时 = 头结点, *被删除结点;

    for(unsigned int 计数 = 0; 计数 < 序号 - 1 && (临时 = 临时->下一个); 计数++)
        ;
    if(临时 == NULL)
        return false;
    else
    {
        if(临时->下一个 == NULL)
            return false;
        else
        {
            被删除结点 = 临时->下一个;
            临时->下一个 = 临时->下一个->下一个;
            free(被删除结点);
            return true;
        }
    }
}

bool 插入结点(struct 结点 *头结点, struct 结点 *被插入的结点, unsigned int  序号)
{
    struct 结点 *临时 = 头结点, *临时位置;

    for(unsigned int 计数 = 0; 计数 < 序号 - 1 && (临时 = 临时->下一个); 计数++)
        ;
    if(临时 == NULL)
        return false;
    else
    {
        临时位置 = 临时->下一个;
        临时->下一个 = 被插入的结点;
        被插入的结点->下一个 = 临时位置;
        return true;
    }
}

int 取链表结点数(struct 结点 *头结点)
{
    struct 结点 *临时 = 头结点;
    unsigned int 计数;

    for(计数 = 0; (临时 = 临时->下一个); 计数++)
        ;
    return 计数;
}

void 追加结点(struct 结点 *头结点, struct 结点 *被追加的结点)
{
    struct 结点 *临时 = 头结点;

    while(临时->下一个)
        临时 = 临时->下一个;
    临时->下一个 = 被追加的结点;
}

void 释放链表(struct 结点 *头结点)
{
    struct 结点 *临时 = 头结点->下一个, *交换;

    while(临时)
    {
        交换 = 临时;
        临时 = 临时->下一个;
        free(交换);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漫漫人生路_SUI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值