我和我的朋友正在做一个像鸭鸭鹅一样工作的项目,我们在使用我们需要使用的链表时遇到问题。从几个测试输入中,据我所知,当我们插入名称(字符串)时似乎存在问题,它没有正确保存到 LL,因此当我们尝试打印时它崩溃了。您可以在此处访问项目信息:http: //tinyurl.com/DuckDuckBoot。
样本输入输出如下:
**Input**
2
3
Bob
Cody
John
2 2
8
Carol
Casey
Nick
Kirsten
Ben
Bo
Billy
Heather
3 4
**Output:**
Game 1:
Cody
Game 2:
Billy
Bo
Carol
Kirsten
Nick
这是代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
char PlayerNames;
struct Node *next;
}
Players;
void insert(Players *ptr, char PlayerNames);
void delete(Players *ptr, char PlayerNames);
void print(Players *Start, Players *ptr);
void main()
{
int NumberOfGames, NumberOfDucks, Rounds, NumberOfPeople, i, j, k, m;
char Names, data;
Players *Start, *temp;
Start = (Players *)malloc(sizeof(Players));
temp = Start;
temp -> next = Start;
scanf("%d", &NumberOfGames);
for (i = 0;i < NumberOfGames;i++)
{
scanf("%d", &NumberOfPeople);
for (j = 0;j < 3;j++)
{
scanf("%s", &Names);
insert(Start, Names);
}
scanf("%d %d", &Rounds, &NumberOfDucks);
for (k = 0;k < Rounds;k++)
{
for (m = 0;m < NumberOfDucks;m++)
Start -> next;
delete(Start, data);
}
}
printf("Game %i:\n", i);
print(Start, Start -> next);
printf("\n");
}
void insert(Players *ptr, char PlayerNames)
{
Players *Start = ptr;
while (ptr -> next != Start)
{
ptr = ptr -> next;
}
ptr -> next = (Players *)malloc(sizeof(Players));
ptr = ptr -> next;
ptr -> PlayerNames = PlayerNames;
ptr -> next = Start;
printf("inserting\n");
return ;
}
void delete(Players *ptr, char PlayerNames)
{
printf("deleting\n");
Players *Start = ptr;
while (ptr -> next != Start && (ptr -> next) -> PlayerNames != PlayerNames)
{
ptr = ptr -> next;
}
Players *temp;
temp = ptr -> next;
ptr -> next = temp -> next;
free(temp);
return ;
}
void print(Players *Start, Players *ptr)
{
printf("printing\n");
if (ptr == Start)
return;
printf("%s", ptr -> PlayerNames);
print(Start, ptr -> next);
return ;
}