您的位置:首页 > 其它

链表之顺序建链表

2016-07-20 16:17 323 查看
#include <stdio.h>

#include <malloc.h>

struct node

{

    int data;

    struct node *next;

};

struct node *creat(struct node *head)

{

    int n;

    scanf("%d",&n);

    struct node *p,*tail;

    head=(struct node *)malloc(sizeof(struct node));

    head->next=NULL;

    tail=head;

    int i;

    for(i=1;i<=n;i++)

    {

        p=(struct node *)malloc(sizeof(struct node));

        scanf("%d",&p->data);

        p->next=NULL;

        tail->next=p;

        tail=p;

    }

    return (head);

}

void main()

{

    struct node *head,*p;

    head=creat(head);

    p=head->next;

    while(p->next)

    {

        printf("%d ",p->data);

        p=p->next;

    }

    printf("%d\n",p->data);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: