SDUT - 2117 数据结构实验之链表二:逆序建立链表

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
int main()
{
    int n, i;
    scanf("%d", &n);
    struct node *h, *p;
    h = (struct node *)malloc(sizeof(struct node ));
    h -> next = NULL;
    for(i =0; i < n; i++)
    {
        p = (struct node *)malloc(sizeof(struct node));
        p -> next = h -> next;
        scanf("%d", &p -> data);
        h -> next = p;
    }
    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/81699733
今日推荐