题解 | 牛牛的单向链表
牛牛的单向链表
https://www.nowcoder.com/practice/95559da7e19c4241b6fa52d997a008c4
#include <stdio.h>
#include <stdlib.h>
// write your code here......
typedef struct array{
int num;
struct array *next;
}node;
void print(node *head){
node*p=head;
while(p!=NULL){
printf("%d ",p->num);
p=p->next;
}
}
void allclean(node **list){
node *p=(*list);
while (p!=NULL) {
(*list)->next=p->next;
free(p);
p=(*list)->next;
}
free(*list);
(*list)=NULL;
}
int main() {
int n;
scanf("%d",&n);
int* arr=(int*)malloc(n*sizeof(int));
for (int i = 0; i < n; i++) {
scanf("%d",&arr[i]);
}
// write your code here......
node *head,*tail,*p;
head=tail=NULL;
for(int i=0;i<n;i++){
p=(node*)malloc(sizeof(node));
p->num=arr[i];
p->next=NULL;
if(head==NULL){
head=p;
}
else {
tail->next=p;
}
tail=p;
}
print(head);
allclean(&head);
free(arr);
return 0;
}

深信服公司福利 851人发布