android-nuc / 18-C-Train

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

丁霆霄(链表倒序)已改 #7

Open dtxdtx opened 5 years ago

dtxdtx commented 5 years ago
#include<stdio.h>
#include<stdlib.h>
#define L sizeof(struct S)
struct S
{
    int num;
    int score;
    struct S *next;
};
int n;
struct S *creat()
{
    struct S *head;
    struct S *p1,*p2;
    head=NULL;
    p1=p2=(struct S*)malloc(L);
    printf("输入链表(以0 0结束)\n");
    scanf("%d%d",&p1->num,&p1->score);
    n=0;
    while(p1->num!=0)
    {
        n=n+1;
        if(n==1)head=p1;
        else
        {
            p2->next=p1;
        }
        p2=p1;
        p1=(struct S*)malloc(L);
        scanf("%d%d",&p1->num,&p1->score);
    }
    p2->next=NULL;
    return head;
}
struct S *unsort(struct S *head)
{
    struct S *p1,*p2;
    p1=head->next;
    p2=head;
    p2->next=NULL;
    if(p1==NULL)return head;
    if(p1->next==NULL)
    {
        head=p1;
        p1->next=p2;
        return head;
    }
    while(p1->next!=NULL)
    {
        head=p1->next;
        p1->next=p2;
        p2=p1;
        p1=head;
    }
    p1->next=p2;
    return head;
}
void print(struct S *head)
{
    struct S *p;
    p=head;
    while (p!=NULL)
    {
        printf("%d %d\n",p->num,p->score);
        p=p->next;
    }
}
int main()
{
    struct S *head;
    head=creat();
    head=unsort(head);
    print(head);
    return 0;
}
dtxdtx commented 5 years ago

No description provided.

wmpscc commented 5 years ago

点评: