简体   繁体   English

c中的链表(我在这里做错了什么?)

[英]linked list in c (what am I doing wrong here?)

I have this piece of code, and I can't figure out why this isn't working? 我有这段代码,我无法弄清楚为什么这不起作用? The inputData() function seems to work, but the print method only prints the first line I send to inputDate() for as many lines that I have inputed. inputData()函数似乎可以工作,但print方法只打印我发送给inputDate()的第一行,因为我输入了很多行。

I'm reading from a file, one line at a time, and putting in to the linked list, that's where the issue is. 我正在读取一个文件,一次一行,并放入链接列表,这就是问题所在。 If I pass values in the code, then there is no problem? 如果我在代码中传递值,那么没有问题?

//LINKED LIST
void inputData(char *l)
{
    struct lines *pNewStruct = (struct lines *) malloc(sizeof(struct lines));
    pNewStruct->line = l;

    //inserts if list empty, next set to null
    if(pFirstNode == NULL){
        pNewStruct->next = NULL;
        pFirstNode = pLastNode = pNewStruct;

    } else {

        //inserts if list contains one element
        //this is done to differentiate between first and last node
        if(pFirstNode == pLastNode) {
            pFirstNode->next = pNewStruct;
            pLastNode = pNewStruct;
            pNewStruct->next = NULL;

        //inserts elements when elements in list > 2
        } else {
            pLastNode->next = pNewStruct;
            pNewStruct->next = NULL;
            pLastNode = pNewStruct;

        }
    }
}

void printData()
{
    struct lines *temp = pFirstNode;

    while(temp != NULL)
    {
        printf("%s", temp->line);
        temp = temp->next;
    }
}

For each line you should dynamically allocate new memory and copy the contents of each line into the newly allocated string. 对于每一行,您应该动态分配新内存并将每行的内容复制到新分配的字符串中。 Otherwise, if no one keeps track of the memory allocated for these strings, or the strings are on the stack, you run the risk of losing them. 否则,如果没有人跟踪为这些字符串分配的内存,或者字符串在堆栈中,则存在丢失它们的风险。

pNewStruct->line = l; pNewStruct-> line = l;

This won't do. 这不行。 And we never see what pFirstNode really is. 我们永远不会看到pFirstNode究竟是什么。

So how about using strcpy for copying the char. 那么如何使用strcpy复制char。

Then the two branches in the else. 然后在其他两个分支。 Why do you think you need them. 为什么你认为你需要它们。 You initiate the pointers while adding the first element. 在添加第一个元素时启动指针。 So this pFirstNode->next = pNewStruct; 所以这个pFirstNode-> next = pNewStruct;

Is the same in the first case as pLastNode->next = pNewStruct; 在第一种情况下与pLastNode-> next = pNewStruct相同;

So there is not need for another if an else branch in the outer else. 因此,如果在其他外部的其他分支,则不需要另一个。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct lines {
  char line [120];
  struct lines *next;
};

struct lines *pFirstNode = NULL;
struct lines *pLastNode = NULL;


//LINKED LIST
void inputData(char *l)
{
    struct lines *pNewStruct = (struct lines *) malloc(sizeof(struct lines));
    strncpy(pNewStruct->line,l,119);


    //inserts if list empty, next set to null
    if(pFirstNode == NULL){
        pNewStruct->next = NULL;
        pFirstNode = pLastNode = pNewStruct;

    } else {
      pLastNode->next = pNewStruct;
      pNewStruct->next = NULL;
      pLastNode = pNewStruct;
    }      
}

void printData()
{
    struct lines *temp = pFirstNode;

    while(temp != NULL)
    {
        printf("%s", temp->line);
        temp = temp->next;
    }
}


int main(void) {
  inputData("one");
  inputData("two");
  inputData("three");
  printData();
  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM