SDUT - 2118 数据结构实验之链表三:链表的逆置

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
int main()
{
    int n = 0, i, k;
    struct node *h, *h1, *q1, *q, *p;
    h1 = (struct node *)malloc(sizeof(struct node ));
    h1 -> next = NULL;
    q1 = h1;
    while(scanf("%d", &k) != EOF)
    {
        if(k == -1)break;
        p = (struct node *)malloc(sizeof(struct node ));
        p -> next = NULL;
        p -> data = k;
        q1 -> next = p;
        q1 = p;
        n++;
    }
    h = (struct node *)malloc(sizeof(struct node ));
    h -> next = NULL;
    q = h;
    q1 = h1 -> next;
    for(i = 0; i < n; i++)
    {
        p = (struct node *)malloc(sizeof(struct node ));
        p -> next = NULL;
        p -> data = q1 -> data;
        p -> next = h -> next;
        h -> next = p;
        q1 = q1 -> next;
    }
    p = h -> next;
    printf("%d", p -> data);
    p = p -> next;
    while(p)
    {
        printf(" %d", p -> data);
        p = p -> next;
    }
    printf("\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Miracle_QSH/article/details/81699683
今日推荐