带头结点的单链表的创建

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

猜你喜欢

转载自www.cnblogs.com/xyfs99/p/10347219.html