android-nuc / 18-C-Train

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

孙雨轩链表倒序改!! #41

Closed artimisgood closed 5 years ago

artimisgood commented 5 years ago

链表倒序

#include"stdio.h"
#include"stdlib.h"
#define ln (sizeof(struct Number))
typedef struct Number
{
    int num;
    struct Number * next, * pre;
}Num;

int a = 0;

Num  *create()
{
    Num *p_new1,*head,*p1;
    p1 = p_new1 = (Num *)malloc(ln);
    head = NULL;
    int m;
    scanf("%d",&m);
    while(m != 0)
    {

        if(a == 0)
        {
            head = p_new1;
            p1 = p_new1;
            head->pre = NULL;
        }
        else
        {
            p_new1->pre = p1;
            p1 -> next = p_new1;
            p1 = p1 -> next;
        }

        a ++;
        p1->num = m;
        scanf("%d",&m);
        p_new1 = (Num *)malloc(ln);

    }
    p1 -> next = NULL;
    return(head);

}

void print(Num * head)
{
    Num * q;
    q = head;
    while(q -> num != 0)
    {
        printf("%d ",q -> num);
        q = q -> next;
    }   

}

void printresort(Num * head)
{
    Num * q;
    q = head;
    while(q->next)
    {
        q = q->next;
    }
    while(q)
    {
        printf("%d ",q->num);
        q = q->pre;
    }

}

int main()
{
    Num *m;
    m = create();
    printresort(m);

    return 0;
}