题解 | #牛牛的单向链表#
牛牛的单向链表
https://www.nowcoder.com/practice/95559da7e19c4241b6fa52d997a008c4
#include <stdio.h> #include <stdlib.h> typedef int data_t ; //定义节点 typedef struct node { data_t date; struct node *next; }listnode,*linklist; //表头创建 linklist list_create() { linklist H ; H = (linklist)malloc(sizeof(listnode)); if(H == NULL) { printf("malloc is NULL"); return NULL; } H->date = 0; H->next = NULL; return H; } //表未插入 void list_insert(linklist H) { linklist L = (linklist)malloc(sizeof(listnode)); linklist h; h = H; if(L == NULL) { printf("L_malloc is NULL"); return ; } int n; scanf("%d",&n); L->date = n; L->next = NULL; while(h->next!=NULL) { h = h->next; } h->next = L; } //打印单链表 int list_show(linklist H) { linklist h = H->next; while (h->next != NULL) { printf("%d ",h->date); h = h->next; } return 0; } int main() { int n, i; linklist H= list_create(); scanf("%d",&n); for(i = 0;i<=n;i++) list_insert(H); list_show(H); return 0; }