题解 | #牛牛的单向链表#
牛牛的单向链表
https://www.nowcoder.com/practice/95559da7e19c4241b6fa52d997a008c4
#include <stdio.h> #include <stdlib.h> //声明结构体 struct List { int date; struct List* next; }; //新节点 struct List* BuyNewCode(int x) { struct List* new=(struct List*)malloc(sizeof(struct List)); new->date=x; new->next=NULL; return new; } //尾插(不带头) void PushBack(struct List** pplist,int x) { struct List* new=BuyNewCode(x); if(*pplist==NULL) { *pplist=new; } else { //用于寻找尾节点的指针 struct List* tail=*pplist; while(tail->next!=NULL) { tail=tail->next; } //找到,链接新节点 tail->next=new; } } //打印输出 void Print(struct List* plist) { struct List* cur=plist; while(cur!=NULL) { printf("%d ",cur->date); cur=cur->next; } } int main() { int n=0; scanf("%d",&n); //不带头指针的链表 struct List* plist=(struct List*)malloc(sizeof(struct List)); plist=NULL; int i=0; int k=0; for(i=0;i<n;i++) { scanf("%d",&k); PushBack(&plist,k); } Print(plist); return 0; }