代碼如下:
# include<stdio.h>
# include <stdlib.h>
# include <string.h>
typedef struct node {
char name[20];
struct node * next;
}ListNode;
typedef ListNode * LinkList;
LinkList InitRing(int totalNumberOfPeople, LinkList linklist)
{
ListNode *footer, *slider;//*footer是表尾,*slider是游標
char name[20];
int i;
footer = NULL;
linklist = slider = (ListNode *)malloc(sizeof(ListNode));
if (linklist == NULL)
{
printf("內存分配錯誤。");
exit(1);
}
getchar();
for (i = 1;i < totalNumberOfPeople;i++)
{
printf("第%d個人的名字\n", i);
scanf("%s",name);
strcpy(slider->name, name);
footer = (ListNode *)malloc(sizeof(LinkList));
if (footer == NULL)
{
printf("內存分配錯誤");
exit(1);
}
slider->next = footer;
slider = footer;
}
printf("第%d個人的名字\n", i);
scanf("%s",name);
strcpy(footer->name, name);
footer->next = linklist;
printf("\n");
return linklist;
}
LinkList DeleteDeath(int totalNumberOfPeople, int countOffLimits, LinkList linklist)
{
int i, j;
ListNode *slider, *deletePeople;
slider = linklist;
for (i = 1;i <= totalNumberOfPeople / 2;i++)
{
for (j = 1;j <= countOffLimits-2;j++)
slider = slider->next;
deletePeople = slider->next;
slider->next = deletePeople->next;
slider = slider->next;
printf("%s ", deletePeople->name);
if (i % 5 == 0) printf("\n");
free(deletePeople);
}
printf("\n");
return linklist;
}
void OutRing(LinkList linklist)
{
LinkList slider;
int i = 1;
slider = linklist;
do {
printf("%s ", slider->name);
if (i % 5 == 0) printf("\n");
i++;
slider = slider->next;
} while (slider != linklist);
}
int main()
{
int totalNumberOfPeople, countOffLimits;
LinkList linklist = NULL;
printf("請輸入總人數:\n");
scanf("%d", &totalNumberOfPeople);
printf("請輸入報數上限:\n");
scanf("%d", &countOffLimits);
linklist = InitRing(totalNumberOfPeople, linklist);//建立循環單鏈鏈表函數
printf("出局名單如下:\n");
linklist = DeleteDeath(totalNumberOfPeople, countOffLimits, linklist);
printf("\n");
printf("生存者名單如下:\n");
OutRing(linklist);
return 0;
}
只要把DeleteDeath函數的free(deletePeople)刪除即可。請問怎麼這是什麼問題,需要怎麼修改?
樓主改了malloc的大小就可以了,當然代碼還是要修改一下的。
貼上修改過的代碼:
# include<stdio.h>
# include <stdlib.h>
# include <string.h>
typedef struct node {
char name[20];
struct node * next;
}ListNode;
typedef ListNode * LinkList;
LinkList InitRing(int totalNumberOfPeople, LinkList linklist)
{
ListNode *footer, *slider;//*footer是表尾,*slider是游標
char name[20];
int i;
footer = NULL;
linklist = slider = (ListNode *)malloc(sizeof(ListNode));
if (linklist == NULL)
{
printf("內存分配錯誤。");
exit(1);
}
getchar();
for (i = 1;i < totalNumberOfPeople;i++)
{
printf("第%d個人的名字\n", i);
scanf("%s",name);
strcpy(slider->name, name);
footer = (ListNode *)malloc(sizeof(ListNode));
if (footer == NULL)
{
printf("內存分配錯誤");
exit(1);
}
slider->next = footer;
slider = footer;
}
printf("第%d個人的名字\n", i);
scanf("%s",name);
strcpy(footer->name, name);
footer->next = linklist;
printf("\n");
linklist = footer;
return linklist;
}
LinkList DeleteDeath(int totalNumberOfPeople, int countOffLimits, LinkList linklist)
{
int i, j;
ListNode *slider, *deletePeople;
slider = linklist;
for (i = 1;i <= totalNumberOfPeople / 2;i++)
{
for (j = 1;j <= countOffLimits-1;j++)
slider = slider->next;
deletePeople = slider->next;
slider->next = deletePeople->next;
printf("%s ", deletePeople->name);
if (i % 5 == 0) printf("\n");
free(deletePeople);
}
printf("\n");
return linklist;
}
void OutRing(LinkList linklist)
{
LinkList slider;
int i = 1;
slider = linklist;
do {
printf("%s ", slider->name);
if (i % 5 == 0) printf("\n");
i++;
slider = slider->next;
} while (slider != linklist);
}
int main()
{
int totalNumberOfPeople, countOffLimits;
LinkList linklist = NULL;
printf("請輸入總人數:\n");
scanf("%d", &totalNumberOfPeople);
printf("請輸入報數上限:\n");
scanf("%d", &countOffLimits);
linklist = InitRing(totalNumberOfPeople, linklist);//建立循環單鏈鏈表函數
printf("出局名單如下:\n");
linklist = DeleteDeath(totalNumberOfPeople, countOffLimits, linklist);
printf("\n");
printf("生存者名單如下:\n");
OutRing(linklist);
return 0;
}