使用链接列表的C错误

时间:2013-09-28 21:46:55

标签: c linked-list

我和我的朋友正在开发一个像鸭鸭一样的项目,我们在使用我们需要使用的链表时遇到了问题。从我可以告诉的几个测试输入中,当我们插入名称(字符串)时,似乎存在问题,它没有正确保存到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 ;
}

0 个答案:

没有答案