数据结构//C——动态分配—离散存储(非循环单链表)

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

struct Student {
    char name[100];
    int age;
    struct Student * pNext;
};
struct Student * Creat_list();
void Out_list(struct Student * pHead);

int main (void){

    struct Student * pHead;
    pHead = Creat_list();
    Out_list(pHead);

    return 0;
}
struct Student * Creat_list(){
    struct Student *pTail = NULL;

    int len;
    printf("请输入学生人数:");
    scanf("%d",&len);

    struct Student * pHead = (struct Student * )malloc(sizeof(struct Student) );
    if(pHead->pNext == NULL || len == 0){
        printf("录入失败,请重新录入!");

    }
//  为什么不是放在上面   if  里面                                     ????
    pHead->pNext = NULL;
    pTail = pHead;

    for (int i = 0; i < len; ++i){
        printf("第%d位学生信息:\n",i+1);

        struct Student * pNew = (struct Student * ) malloc(sizeof(struct Student));
        printf("姓名:");
        scanf("%s", pNew->name);
        printf("年龄:");
        scanf("%d",&(pNew->age));

        pTail->pNext = pNew;
        pNew->pNext = NULL;
        pTail = pNew;
    }
    return pHead;
}
void Out_list(struct Student * pHead){
//  q == pNew
    struct Student * q = pHead->pNext;
    if(q != NULL)
        printf("学生信息如下:\n");
    while (q != NULL){
        printf("姓名:%s  年龄:%d\n",q->name, q->age);
        q = q->pNext;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值