Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我的链接列表的标题被覆盖_C_Linked List - Fatal编程技术网

C 我的链接列表的标题被覆盖

C 我的链接列表的标题被覆盖,c,linked-list,C,Linked List,我正在尝试创建一个包含姓名和出生日期信息的人的链接列表。person信息在一个结构中声明,当我创建一个新节点时,该新节点稍后将添加到列表的末尾。发生的事情是,我在insertNode函数中得到了一个无限循环,因为不知怎么的,我的列表的头在我的createNode函数中被改变了。有时我所做的工作有效,而有时却无效,这让我相信我在做一些我没有抓住的小错误 我把代码贴在下面。还有更多的主要,但我没有张贴,因为它的罚款,直到这一点 typedef struct info { char *firs

我正在尝试创建一个包含姓名和出生日期信息的人的链接列表。person信息在一个结构中声明,当我创建一个新节点时,该新节点稍后将添加到列表的末尾。发生的事情是,我在insertNode函数中得到了一个无限循环,因为不知怎么的,我的列表的头在我的createNode函数中被改变了。有时我所做的工作有效,而有时却无效,这让我相信我在做一些我没有抓住的小错误

我把代码贴在下面。还有更多的主要,但我没有张贴,因为它的罚款,直到这一点

typedef struct info
{
    char *firstName;
    char *lastName;
    int month;
    int day;
    int year;
    struct info *next;
} person;

person *createNode(char *fName, char *lName, char *m, int d, int y)
{
    person *n = malloc(sizeof(person));
    n->firstName = strdup(fName);
    n->lastName = strdup(lName);
    n->month = convertMonthCharToInt(m);
    n->day = d;
    n->year = y;
    n->next = NULL;

    return n;
}

person *insertNode(person *head, person *newNode)
{
    person *temp = NULL;
    temp = head;

    if(head == NULL)
        return newNode;

    for(temp = head; temp->next!=NULL; temp = temp->next);

    if(head->next == NULL)
        head->next = newNode;

    return head;
}

int main()

{
    int numClasses = 0, numStudents = 0, numQueries = 0, d = 0, y = 0, j = 0;
    char fName[31], lName[31], m[11], queryFirstName[31], queryLastName[31];
    person *print;

    FILE *readFile = fopen("birthday2.txt", "r");

    if(readFile == NULL)
    {
        printf("File does not exist. Closing program.\n");
        return 0;
    }

    fscanf(readFile, "%d", &numClasses);

    while(i<numClasses)
    {
        person *head = malloc(sizeof(person));
        person *newNode = malloc(sizeof(person));
        person *result = malloc(sizeof(person));
        head = NULL;
        fscanf(readFile, "%d", &numStudents);
        j = 0;
        while(j<numStudents)
        {
            //read names in from file
            fscanf(readFile, "%s", &fName);
            fscanf(readFile, "%s", &lName);
            fscanf(readFile, "%s", &m);
            fscanf(readFile, "%d", &d);
            fscanf(readFile, "%d", &y);
            fscanf(readFile, "%d", &numQueries);

            //creates a node. My list head changes from here
            newNode = createNode(fName, lName, m, d, y);
            //to here. So I'm pretty sure it's changing within this function. Just not sure why.
            head = insertNode(head, newNode);
}
typedef结构信息
{
char*名字;
char*lastName;
整月;
国际日;
国际年;
结构信息*下一步;
}人;
person*createNode(char*fName,char*lName,char*m,int d,int y)
{
person*n=malloc(sizeof(person));
n->firstName=strdup(fName);
n->lastName=strdup(lName);
n->month=convertmonthChartPoint(m);
n->d=d;
n->year=y;
n->next=NULL;
返回n;
}
person*insertNode(person*head,person*newNode)
{
person*temp=NULL;
温度=水头;
if(head==NULL)
返回newNode;
对于(临时=头部;临时->下一步!=NULL;临时=临时->下一步);
if(head->next==NULL)
head->next=newNode;
回流头;
}
int main()
{
int numclass=0,numStudents=0,numquerys=0,d=0,y=0,j=0;
char fName[31]、lName[31]、m[11]、queryFirstName[31]、queryLastName[31];
个人*印刷品;
FILE*readFile=fopen(“birthday2.txt”、“r”);
if(readFile==NULL)
{
printf(“文件不存在。正在关闭程序。\n”);
返回0;
}
fscanf(readFile、%d、&numclass);

虽然(i在
insertNode
函数中,通过for循环迭代到列表的末尾,但不将节点添加到列表的末尾。您需要添加
temp->next=newNode
。不喜欢名称
insertNode
as
insert
给人的印象是您提供了一个用于插入项的索引。
>Add
Append
作为函数名更具意义。另外,主要是为
person*head
分配空间,并立即将其设置为NULL。如果希望指针指向NULL或现有数据,则无需为其分配空间。您只需
malloc
来创建新数据。是的,我的错。temp->next part之前就在那里。我拿了它几秒钟,看看是否可以让它递归地插入,只是为了好玩。问题是我的头指针在我的createNode函数中发生了变化,我不知道为什么。看起来你缺少代码。你可能想更新它的其余部分。我在main中添加了一行缺失的内容。main af中的其他所有内容都丢失了这是合并排序的东西,并找出谁的生日最接近。只要我的列表的标题是正确的,这些东西都会起作用。现在,不是这样。我想你不需要
person*head=malloc(sizeof(person));
person*newNode=malloc(sizeof(person))在外部循环的开始处。只需将两者初始化为NULL。实际上,第一个
malloc
调用分配的内存实际上会泄漏,因为之后直接覆盖指针。