user-0 提问: C语言；n个节点的链表中随机取m个节点（无重复）组成一个新链表，，并打印出来，写出来无错误，但运行不了，能帮忙改下吗，救救孩子

回复:
user: user-0
content: 引用 4 楼 自信男孩 的回复：#include<stdio.h> #include<stdlib.h> #include <time.h> //int n,m; //int r,q;  typedef struct node {     int data;     struct node*next; }LA; /*    typedef struct node1    {    int data1;    struct node1*next1;    }LA1;    */ //LA* head, head1; //LA1*head1; //LA1* g; //LA* h; //  LA *create_link(void) {     LA *p, *phead, *last;     int i, n;      printf("input n");     scanf("%d", &n);      phead=NULL;     for(i=1;i<=n;i++)     {         p = (LA*)malloc(sizeof(LA));         p->data = i;         p->next = NULL;          if (!phead) {             phead = p;             last = phead;             continue;         }         last->next = p;         last = p;     }      return phead; }  void show_link(LA *phead) {     LA *pcur = phead;      while (pcur) {         printf("%d\t", pcur->data);         pcur = pcur->next;     }     putchar(10); }  int count_link_len(LA *phead) {     LA *pcur = phead;     int cnt = 0;      while (pcur) {         cnt++;         pcur = pcur->next;     }      return cnt; }  LA *find_key_node(LA **phead, int key) {     LA *prev, *pcur;      pcur = prev = *phead;     while (pcur) {         if (key == pcur->data) {             if (pcur == *phead)                 *phead = (*phead)->next;             else                 prev->next = pcur->next;             return pcur;         }         prev = pcur;         pcur = pcur->next;     }      return NULL; }  LA *create_new_link(LA *phead) {     LA *new_phead = NULL, *pnew, *last;     int len, key, m, i;      srand(time(NULL));     len = count_link_len(phead);     printf("Please input m: ");     scanf("%d", &m);      if (m >= len)         m = len;      i = 0;     while (i < m) {         key = rand() % len + 1;         pnew = find_key_node(&phead, key);         if (!pnew)             continue;         printf("key = %d\n", key);         if (!new_phead) {             new_phead = pnew;             last = new_phead;             i++;             continue;         }         last->next = pnew;         last = pnew;         i++;     }     last->next = NULL;      return new_phead; }    int main() {     LA *phead, *new_phead;      phead = create_link();     show_link(phead);     new_phead = create_new_link(phead);     show_link(new_phead);      return 0; }  供参考～  建议把代码写的更清晰一些，如果把功能放在一起，比较混乱。跪谢了，，救世主啊，我会将代码分析写好，好好学编程的

user: user-1
content: #include<stdio.h> #include<stdlib.h> #include <time.h> //int n,m; //int r,q;  typedef struct node {     int data;     struct node*next; }LA; /*    typedef struct node1    {    int data1;    struct node1*next1;    }LA1;    */ //LA* head, head1; //LA1*head1; //LA1* g; //LA* h; //  LA *create_link(void) {     LA *p, *phead, *last;     int i, n;      printf("input n");     scanf("%d", &n);      phead=NULL;     for(i=1;i<=n;i++)     {         p = (LA*)malloc(sizeof(LA));         p->data = i;         p->next = NULL;          if (!phead) {             phead = p;             last = phead;             continue;         }         last->next = p;         last = p;     }      return phead; }  void show_link(LA *phead) {     LA *pcur = phead;      while (pcur) {         printf("%d\t", pcur->data);         pcur = pcur->next;     }     putchar(10); }  int count_link_len(LA *phead) {     LA *pcur = phead;     int cnt = 0;      while (pcur) {         cnt++;         pcur = pcur->next;     }      return cnt; }  LA *find_key_node(LA **phead, int key) {     LA *prev, *pcur;      pcur = prev = *phead;     while (pcur) {         if (key == pcur->data) {             if (pcur == *phead)                 *phead = (*phead)->next;             else                 prev->next = pcur->next;             return pcur;         }         prev = pcur;         pcur = pcur->next;     }      return NULL; }  LA *create_new_link(LA *phead) {     LA *new_phead = NULL, *pnew, *last;     int len, key, m, i;      srand(time(NULL));     len = count_link_len(phead);     printf("Please input m: ");     scanf("%d", &m);      if (m >= len)         m = len;      i = 0;     while (i < m) {         key = rand() % len + 1;         pnew = find_key_node(&phead, key);         if (!pnew)             continue;         printf("key = %d\n", key);         if (!new_phead) {             new_phead = pnew;             last = new_phead;             i++;             continue;         }         last->next = pnew;         last = pnew;         i++;     }     last->next = NULL;      return new_phead; }    int main() {     LA *phead, *new_phead;      phead = create_link();     show_link(phead);     new_phead = create_new_link(phead);     show_link(new_phead);      return 0; }  供参考～  建议把代码写的更清晰一些，如果把功能放在一起，比较混乱。

user: user-0
content: 昨天改了一晚上还不行

user: user-0
content: 自学的链表。可能写的不太好。

user: user-0
content: 救救孩子吧。。。。大佬们