数据结构之顺序建链表

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node*next;
};
struct node * creat(int n)
{
 struct node *head,*tail,*p;
 int i;
 head=(struct node*)malloc(sizeof(struct node));
 head ->next=NULL;
 tail=head;
 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 print(struct node * head)
{
    struct node *p;
    p=head->next;
    while(p!=NULL)
    {
        printf("%d",p->data);
        if(p->next!=NULL)
        {
            printf(" ");
        }
        else
        {
            printf("\n");
        }
        p=p->next;
    }

}
int main()
{
    int N;
    struct node * head;
    scanf("%d",&N);
    head=creat(N);
    print(head);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41341757/article/details/81746214