题解 | #牛牛的单向链表#
牛牛的单向链表
https://www.nowcoder.com/practice/95559da7e19c4241b6fa52d997a008c4
#include <stdio.h> #include <stdlib.h> #include <assert.h> typedef struct list { int date; struct list* next; }L; L* SLTBuyNode(int x) { L* newnode = (L*)malloc(sizeof(L)); if (newnode == NULL) { perror("malloc fail!"); exit(1); } newnode->date = x; newnode->next = NULL; return newnode; } void insert(L** phead,int n) { assert(phead); L* newnode = SLTBuyNode(n); if (*phead == NULL) { *phead = newnode; } else { L* ptail = *phead; while (ptail->next) { ptail = ptail->next; } ptail->next = newnode; } } void print(L* phead) { L* pcur = phead; while (pcur) { printf("%d ", pcur->date); pcur = pcur->next; } } int main() { int x; scanf("%d",&x); L* head; head=NULL; for(int i=0;i<x;i++) { int n; scanf("%d",&n); insert(&head,n); } print(head); return 0; }