题解 | #牛牛的链表添加节点#
牛牛的链表添加节点
https://www.nowcoder.com/practice/e33b79c2e15a41f9b541e73cd256124a
#include <stdio.h> #include<stdlib.h> typedef int data_t; typedef struct node { data_t data; struct node *next; } listnode,*linklist; linklist list_create() { linklist H = (linklist)malloc(sizeof(listnode)); if(H == NULL) { printf("malloc is failed"); return NULL; } H->data = 0; H->next = NULL; return H; } void list_insert(linklist H) { linklist L = (linklist)malloc(sizeof(listnode)); linklist h = H; while(h->next != NULL) h = h->next; int value; scanf("%d",&value); h->next = L; L->data = value; L->next = NULL; } void list_show(linklist H) { linklist h = H->next; while(h) { printf("%d ",h->data); h = h->next; } } void list_input(linklist H,int n) { linklist h = H; linklist L = (linklist)malloc(sizeof(listnode)); L->data = n; int i; for(i = 0;i<n;i++) h = h->next; if(h->next == NULL) { h->next = L; L->next = NULL; } else { L->next = h->next; h->next = L; } } int main() { int i,n, value; linklist H = list_create(); scanf("%d %d",&n,&value); for(i = 0; i<n;i++) list_insert(H); list_input(H, value); list_show(H); return 0; }