【无标题】C语言简单的头插法

阅读 28

2022-03-11

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

精彩评论(0)

0 0 举报