C语言链表

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

#define ERR 1
#define OK  0

typedef struct stu_info_t STU_INFO_T;
extern STU_INFO_T *linklist_init();
extern STU_INFO_T *stu_info_add(int n, STU_INFO_T *p_head);

typedef struct stu_info_t
{
    unsigned int age;
    char name[50];
    float grade;
    char stu_class[50];
    struct stu_info_t *p_next;
}STU_INFO_T;

STU_INFO_T *linklist_init()
{
    STU_INFO_T *p_head = NULL;
    STU_INFO_T *p_cur = NULL;

    p_head = (STU_INFO_T *)malloc(sizeof(STU_INFO_T));

    if (p_head == NULL)
    {
        printf("<%s:%d>memory request error!\n", __FUNCTION__, __LINE__);
        exit(ERR);
    }

    p_cur = p_head;
    p_cur->p_next = NULL;

    return p_head;
}

STU_INFO_T *stu_info_add(int n, STU_INFO_T *p_head)
{
    int i = 0;
    STU_INFO_T *p_tmp = NULL;
    STU_INFO_T *p_cur = NULL;
    char name[50] = {0};
    int age = 0;
    char stu_class[50] = {0};
    float grade = 0;

    p_cur = p_head;

    for(i = 0; i < n; i++)
    {
        p_tmp = (STU_INFO_T *)malloc(sizeof(STU_INFO_T));
        if (p_tmp == NULL)
        {
            printf("<%s:%d>memory request error!\n", __FUNCTION__, __LINE__);
            exit(ERR);
        }

        memset(name, 0, sizeof(name));

        printf("please input the student[%d] name:\n", i);
        scanf("%s", name);
        printf("please input the student[%d] age:\n", i);
        scanf("%d", &age);
        printf("please input the student[%d] class:\n", i);
        scanf("%s", stu_class);
        printf("please input the student[%d] grade:\n", i);
        scanf("%f", &grade);

        memcpy(p_tmp->name, name, 50);
        memcpy(p_tmp->stu_class, stu_class, 50);
        p_tmp->age = age;
        p_tmp->grade = grade;

        p_tmp->p_next = NULL;
        p_cur->p_next = p_tmp;
        p_cur = p_tmp;
    }

    return p_head;
}

int stu_info_prt(STU_INFO_T *p_head)
{
    STU_INFO_T *p_tmp = NULL;

    system("CLS");
    printf("\
        name\
        age\
        class\
        grade\n");

    p_tmp= p_head;
    p_tmp = p_tmp->p_next;

    if (NULL == p_tmp)
    {
        printf("linklist is empty!\n");
        return OK;
    }

    while (NULL != p_tmp)
    {
        printf("%14s %12d %15s %10.2f\n",
               p_tmp->name,
               p_tmp->age,
               p_tmp->stu_class,
               p_tmp->grade);
        p_tmp = p_tmp->p_next;
    }

    return OK;
}

int main()
{
    int sel = 0;
    int n = 0;
    STU_INFO_T *p_head = NULL;

    p_head = linklist_init();

    while (1)
    {
        printf("\n***************************************************\n");
        printf("***************************************************\n");
        printf("********                                   ********\n");
        printf("********                                   ********\n");
        printf("********        stu info manag sys         ********\n");
        printf("********                                   ********\n");
        printf("********        1.info_dispaly             ********\n");
        printf("********        2.info_add                 ********\n");
        printf("********        3.info_del                 ********\n");
        printf("********        4.info_save                ********\n");
        printf("********        5.exit                     ********\n");
        printf("********                                   ********\n");
        printf("***************************************************\n");
        printf("***************************************************\n");

        printf("please make your choice:\n");
        scanf("%d", &sel);

        switch(sel)
        {
            case 1:
            {
                (void)stu_info_prt(p_head);
                break;
            }
            case 2:
            {
                printf("please input the number of students:\n");
                scanf("%d", &n);

                (void)stu_info_add(n, p_head);
                break;
            }
            case 5:
            {
                exit(0);
            }
            default:
            {
                break;
            }
        }
    }

    return OK;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值