android-nuc / 18-C-Train

18级Android实验室(人工智能+移动互联) C语言培训
16 stars 10 forks source link

许志茹 链表 倒序 #19

Closed xzr912 closed 5 years ago

xzr912 commented 5 years ago

#include<stdio.h>
#include<stdlib.h>
struct Student{
    int num;
    char name[20];
    struct Student *next;
};
struct Student *creat()
{
    struct Student *head = NULL,*end,*pnew;
    pnew=(struct Student*)malloc(sizeof(struct Student));
    scanf("%d",&pnew->num);
    scanf("%s",&pnew->name);
    int count = 0;
    while(pnew->num != 0)
    {
        count ++;
        if(1 == count)
        {
            pnew->next = head;
            head = pnew;
            end = pnew;
        }
        else
        {
            end->next = pnew;
            end = pnew;
        }
        pnew = (struct Student*)malloc(sizeof(struct Student));
        scanf("%d",&pnew->num);
        scanf("%s",&pnew->name);
    }
    end->next = NULL;
    free(pnew);
    return (head);
}
struct Student *reserve(struct Student *head)
{
    struct Student *p,*pnew = NULL;
    if(head == NULL && head->next == NULL)
    {
        return head;
    }
    p = head;
    while(p != NULL)
    {
        struct Student *temp = p->next;
        p->next = pnew;
        pnew = p;
        p = temp;

    }
    return (pnew);
}
void print(struct Student *head)
{
    struct Student *p;
    p = head;
    while(p != NULL)
    {
        printf("学号%d  ",p->num);
        printf("姓名%s\n",p->name);
        p = p->next;
    }
}
int main()
{
    struct Student *pt;
    pt = creat();
    pt = reserve(pt);
    print(pt);
    system("pause");
    return 0;
}
wmpscc commented 5 years ago

点评:

通过!