带头节点链表的前插法,后插法,顺序添加法

本文介绍了一个使用C语言实现的学生信息管理系统,通过链表来存储和管理学生数据,包括添加、删除、显示等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

typedef struct student
{
    long id;
    char name[32];
    char sex[8];
    int age;
    float math;
    struct student *next;//指向下一个节点的指针
}stu,*pstu;

stu *head;//全局变量在main函数执行之前初始化,指针默认初始化为NULL

//函数声明
stu *frontadd(stu *head);
stu *rearadd(stu *head);
stu *squadd(stu *head);
stu *delete(stu *head);
void print_list(stu *head);
void free_list(stu *head);

int main()
{
    int sel=0;
    //先给头节点申请内存空间,但不存值
    head=(stu *)malloc(sizeof(stu));
    if(NULL==head)
    {
        printf("head malloc failed\n");
        return -1;
    }
    head->next=NULL;//next指针必须赋空

    while(1)
    {
        printf("\t1:前插法添加节点\n\t2:后插法添加节点\n\t3:顺序添加节点\n\t4:删除节点\n\t5:查看节点\n\t6:退出\n");
        printf("请输入选项:");
        scanf("%d",&sel);
        getchar();//取出缓冲区中的'\n'
        switch(sel)
        {
            case 1:
                head=frontadd(head);
                break;
            case 2:
                head=rearadd(head);
                break;
            case 3:
                head=squadd(head);
                break;
            case 4:
                head=delete(head);
                break;
            case 5:
                print_list(head);
                break;
            case 6:
                //释放链表内存
                free_list(head);
                exit(0);
            default:
                printf("reinput 1-6\n");        
        }
    }
    return 0;
}
stu *frontadd(stu *head)
{
    if(NULL==head)
    {
        printf("head null\n");
        return NULL;
    }

    while(1)
    {
        stu *s=NULL;
        s=(stu *)malloc(sizeof(stu));
        if(NULL==s)
        {
            printf("s malloc failed\n");
            return NULL;
        }
        s->next=NULL;
        printf("请输入学号:\n");
        scanf("%ld",&s->id);
        printf("请输入姓名:\n");
        scanf("%s",s->name);
        printf("请输入性别:\n");
        scanf("%s",s->sex);
        printf("请输入年龄:\n");
        scanf("%d",&s->age);
        printf("请输入数学成绩:\n");
        scanf("%f",&s->math);

        s->next=head->next;
        head->next=s;

        char judge;
        while(getchar()!='\n');//清空缓冲区
        printf("是否继续添加(y/n):");
        judge=getchar();
        if(judge=='y')
        {
            continue;
        }
        else
        {
            break;
        }
    }

    return head;
}

stu *rearadd(stu *head)
{
    if(NULL==head)
    {
        printf("head null\n");
        return NULL;
    }

    while(1)
    {
        stu *s=NULL;
        s=(stu *)malloc(sizeof(stu));
        if(NULL==s)
        {
            printf("s malloc failed\n");
            return NULL;
        }
        s->next=NULL;
        printf("请输入学号:\n");
        scanf("%ld",&s->id);
        printf("请输入姓名:\n");
        scanf("%s",s->name);
        printf("请输入性别:\n");
        scanf("%s",s->sex);
        printf("请输入年龄:\n");
        scanf("%d",&s->age);
        printf("请输入数学成绩:\n");
        scanf("%f",&s->math);

        //添加节点
        stu *p=NULL;
        stu *q=NULL;
        q=p=head;
        while(p!=NULL)
        {
            q=p;
            p=p->next;
        }

        q->next=s;

        char judge;
        while(getchar()!='\n');//清空缓冲区
        printf("是否继续添加(y/n):");
        judge=getchar();
        if(judge=='y')
        {
            continue;
        }
        else
        {
            break;
        }
    }
    return head;
}

stu *squadd(stu *head)
{
    if(NULL==head)
    {
        printf("head null\n");
        return NULL;
    }

    while(1)
    {
        stu *s=NULL;
        s=(stu *)malloc(sizeof(stu));
        if(NULL==s)
        {
            printf("s malloc failed\n");
            return NULL;
        }
        s->next=NULL;
        printf("请输入学号(大于0):\n");
        scanf("%ld",&s->id);
        printf("请输入姓名:\n");
        scanf("%s",s->name);
        printf("请输入性别:\n");
        scanf("%s",s->sex);
        printf("请输入年龄:\n");
        scanf("%d",&s->age);
        printf("请输入数学成绩:\n");
        scanf("%f",&s->math);

        //添加节点
        stu *p=NULL;
        stu *q=NULL;
        q=p=head;
        head->id=0;
        while((p!=NULL)&&(s->id>p->id))
        {
            q=p;
            p=p->next;
        }
        q->next=s;
        s->next=p;        

        char judge;
        while(getchar()!='\n');//清空缓冲区
        printf("是否继续添加(y/n):");
        judge=getchar();
        if(judge=='y')
        {
            continue;
        }
        else
        {
            break;
        }
    }
    return head;
}

stu *delete(stu *head)
{
    if(NULL==head)
    {
        printf("delete head null\n");
        return NULL;
    }

    while(1)
    {
        if(NULL==head->next)
        {
            printf("valid list empyt\n");
            return head;
        }

        long x=0;
        printf("请输入删除的数据:\n");
        scanf("%ld",&x);

        stu *p=NULL;
        stu *q=NULL;
        q=p=head;
        //q=p=head->next;
        while((p!=NULL)&&(p->id!=x))
        {
            q=p;
            p=p->next;
        }

        if(NULL==p)
        {
            printf("this %ld not in list\n",x);
            return head;
        }
        else
        {
            q->next=p->next;
            free(p);
            p=NULL;
        }

        char judge;
        printf("是否继续删除(y/n):");
        while(getchar()!='\n');
        judge=getchar();
        if(judge=='y')
        {
            continue;
        }
        else
        {
            break;
        }
    }
    return head;
}

void print_list(stu *head)
{
    if(NULL==head)
    {
        printf("head null\n");
        return;
    }

    if(NULL==head->next)
    {
        printf("valid list empty\n");
        return;
    }

    stu *p=NULL;
    p=head->next;
    printf("学号\t姓名\t性别\t年龄\t数学\n");
    while(p!=NULL)
    {
        printf("%ld\t%s\t%s\t%d\t%.2f\n",p->id,p->name,p->sex,p->age,p->math);
        p=p->next;
    }
    return;
}

void free_list(stu *head)
{
    if(NULL==head)
    {
        printf("head null\n");
        return;
    }

    stu *p=NULL;
    stu *q=NULL;

    p=head;
    while(p!=NULL)
    {
        q=p;
        p=p->next;
        free(q);
        q=NULL;
    }

    return;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值