SDUT - 2116 数据结构实验之链表一:顺序建立链表

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

猜你喜欢

转载自blog.csdn.net/Miracle_QSH/article/details/81699563