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

2116-数据结构实验之链表一:顺序建立链表 SDUT C语言_第1张图片
注:最后输出的数据后有空格

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

你可能感兴趣的:(2116-数据结构实验之链表一:顺序建立链表 SDUT C语言)