题解 | 牛牛的单向链表
牛牛的单向链表
https://www.nowcoder.com/practice/95559da7e19c4241b6fa52d997a008c4
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node* next;
}NODE;
NODE* Initnode()
{
NODE* head=(NODE*)malloc(sizeof(NODE));
head->data=0;
head->next=NULL;
return head;
}
void Insernode(int arr[],NODE* L,int a)
{
int i;
NODE* b=L;
for(i=0;i<a;i++)
{
while(b->next!=NULL)
{
b=b->next;
}
NODE* p=(NODE*)malloc(sizeof(NODE));
b->next=p;
p->next=NULL;
p->data=arr[i];
}
}
void Printnode(NODE* L)
{
NODE* p=L->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}
int main()
{
int a;
scanf("%d",&a);
int arr[a];
int i;
for(i=0;i<a;i++)
{
scanf("%d",&arr[i]);
}
NODE* L=Initnode();
Insernode(arr,L,a);
Printnode(L);
}
