user-0 提问: C语言双向约瑟夫问题，输出那边不清楚哪里出错了，free也有问题，求各位大神帮忙看一下程序TUT

回复:
user: user-0
content: 引用 1 楼 cfjtaishan 的回复:#include <stdio.h> #include <string.h> #include <stdlib.h>  struct seat{     int num;     struct seat * prior;     struct seat * next; };  void create_link(struct seat **pphead, int n) {     int i;     struct seat *p, *pcur;      if (!(*pphead)) {         *pphead = (struct seat * ) malloc (sizeof(struct seat));         if (!(*pphead)) {             fprintf(stderr, "malloc error!\n");             exit(0);         }     }     (*pphead)->num = n;     (*pphead)->next = *pphead;     (*pphead)->prior = *pphead;      pcur = *pphead;     for (i = n-1; i > 0; i--) {         p = (struct seat *)malloc(sizeof(struct seat));         if (!p) {             fprintf(stderr, "malloc error!\n");             exit(0);         }         p->num = i;         p->prior = pcur;         p->next = pcur->next;         pcur->next = p;         pcur = p;     } }  void show_list(struct seat *phead) {     struct seat *pcur = phead;      printf("Positive: ");     while (pcur->next != phead) {         printf("%d\t", pcur->num);         pcur = pcur->next;     }     printf("%d\n", pcur->num);      printf("Reverse: ");     while (pcur != phead) {         printf("%d\t", pcur->num);         pcur = pcur->prior;     }     printf("%d\n", pcur->num); }  struct seat *delete_link(struct seat *phead, int start, int num) {     struct seat *pcur = phead, *pdel;     int i = 0;      while (i++ < start)         pcur = pcur->next;      i = 0;     while (pcur->next != pcur) {         if ((i+1) % num == 0) {             pdel = pcur;             pcur->prior->next = pcur->next;             pcur->next->prior = pcur->prior;             printf("%d is out!\n", pdel->num);             pcur = pcur->next;             i++;             free(pdel);         } else {             i++;             pcur = pcur->next;         }     }      return pcur; }  int main(void) {     int n, start, m;     struct seat *phead = NULL, *last = NULL;      printf("Please input n, start  and m(n,start,m): ");     scanf("%d,%d,%d", &n, &start, &m);      if (start > n) {         fprintf(stdout, "num shold be bigger than start!\n");         return 0;     }     create_link(&phead, n);     show_list(phead);      last = delete_link(phead, start, m);     printf("Last node: %d\n", last->num);     free(last);     phead = NULL;      return 0; }  参考一下吧～ 代码都写到main函数里不是一种好的风格，建议根据功能划分，运用不同的子函数去实现每个划分的功能，然后在main函数里调用。 对于这个需求，建议采用无哨兵的双向循环链表  好的谢谢~

user: user-1
content: #include <stdio.h> #include <string.h> #include <stdlib.h>  struct seat{     int num;     struct seat * prior;     struct seat * next; };  void create_link(struct seat **pphead, int n) {     int i;     struct seat *p, *pcur;      if (!(*pphead)) {         *pphead = (struct seat * ) malloc (sizeof(struct seat));         if (!(*pphead)) {             fprintf(stderr, "malloc error!\n");             exit(0);         }     }     (*pphead)->num = n;     (*pphead)->next = *pphead;     (*pphead)->prior = *pphead;      pcur = *pphead;     for (i = n-1; i > 0; i--) {         p = (struct seat *)malloc(sizeof(struct seat));         if (!p) {             fprintf(stderr, "malloc error!\n");             exit(0);         }         p->num = i;         p->prior = pcur;         p->next = pcur->next;         pcur->next = p;         pcur = p;     } }  void show_list(struct seat *phead) {     struct seat *pcur = phead;      printf("Positive: ");     while (pcur->next != phead) {         printf("%d\t", pcur->num);         pcur = pcur->next;     }     printf("%d\n", pcur->num);      printf("Reverse: ");     while (pcur != phead) {         printf("%d\t", pcur->num);         pcur = pcur->prior;     }     printf("%d\n", pcur->num); }  struct seat *delete_link(struct seat *phead, int start, int num) {     struct seat *pcur = phead, *pdel;     int i = 0;      while (i++ < start)         pcur = pcur->next;      i = 0;     while (pcur->next != pcur) {         if ((i+1) % num == 0) {             pdel = pcur;             pcur->prior->next = pcur->next;             pcur->next->prior = pcur->prior;             printf("%d is out!\n", pdel->num);             pcur = pcur->next;             i++;             free(pdel);         } else {             i++;             pcur = pcur->next;         }     }      return pcur; }  int main(void) {     int n, start, m;     struct seat *phead = NULL, *last = NULL;      printf("Please input n, start  and m(n,start,m): ");     scanf("%d,%d,%d", &n, &start, &m);      if (start > n) {         fprintf(stdout, "num shold be bigger than start!\n");         return 0;     }     create_link(&phead, n);     show_list(phead);      last = delete_link(phead, start, m);     printf("Last node: %d\n", last->num);     free(last);     phead = NULL;      return 0; }  参考一下吧～ 代码都写到main函数里不是一种好的风格，建议根据功能划分，运用不同的子函数去实现每个划分的功能，然后在main函数里调用。 对于这个需求，建议采用无哨兵的双向循环链表