【问题标题】:store struct of info in single linked list在单链表中存储信息结构
【发布时间】:2021-05-27 10:52:57
【问题描述】:

我正在尝试将人员信息结构存储到单个链表中并对其执行一些操作

我的结构信息是

struct Person {
    char name[MAX];
    char id[MAX];
};

而我的单链表是使用 struct like 定义的

struct Node {
    struct Person* pPerson;
    struct Node* pNext;
};

我的想法是通过获取用户的输入来定义一个Person 结构

struct Person* newPerson() {
    Person* pPerson = NULL;
    pPerson = (Person*)malloc(sizeof(Person));
    printf("name: "); scanf("%s", pPerson->name);
    printf("id: "); scanf("%s", pPerson->id);
    
    return pPerson;
}

这是initNode 新创建节点的函数

struct Node* newNode(Person* pNewPerson) {
    Node* pPeople = NULL;
    pPeople = (struct Node*)malloc(sizeof(struct Node));
    pPeople->pPerson = pNewPerson;
    return pPeople;
}

这是我的 insert 函数,将结构体 Person 存储在列表中

void insert(Node* pHead, Person* pNewPerson) {
    Node* pCurrent = pHead;
    while (pCurrent != NULL) {
        pCurrent = pCurrent->pNext;
    }
    
    pCurrent = (Node*)malloc(sizeof(Node));
    pCurrent->pNext->pPerson = pNewPerson;
    pCurrent->pNext = NULL;
}

最后是display函数

void display(Node* pHead) {
    Node* pCurrent = pHead;
    int index = 1;
    
    while (pCurrent != NULL) {
        printf("Person %d\n", index);
        printf("name: %s\n", pCurrent->pPerson->name);
        printf("id: %s\n", pCurrent->pPerson->id);
        index += 1;
        pCurrent = pCurrent->pNext;
    }
}

我只是测试,但我的代码中某处似乎有问题

    Node* pNode = NULL;
    Person* pPerson = NULL;
    pPerson = newPerson();
    pNode = newNode(pPerson);
    insert(pNode, pPerson);
    display(pNode);

当我得到输入值但输出是segmentation default core dump。 我是指针的新手,我只是按照逻辑将指针分配到需要的位置。

有人可以纠正我吗?

EDIT: 问题解决了

struct Person* newPerson() {
    Person* pPerson = NULL;
    pPerson = (Person*)malloc(sizeof(Person));
    printf("name: "); scanf("%s", pPerson->name);
    printf("id: "); scanf("%s", pPerson->id);
    
    return pPerson;
}

struct Node* newNode(Person* pNewPerson) {
    Node* pPeople = (struct Node*)malloc(sizeof(struct Node));
    pPeople->pPerson = pNewPerson;
    pPeople->pNext = NULL;
    return pPeople;
}
void insert(Node* pHead, Person* pNewPerson) {
    Node* pCurrent = pHead;
    if (pCurrent == NULL)
    {
        return pNewNode;
    }
    while (pCurrent-> pNext != NULL) {
        pCurrent = pCurrent->pNext;
    }
    pCurrent->pNext = pNewNode;
}

void display(Node* pHead) {
    Node* pCurrent = pHead;
    int index = 1;
    
    while (pCurrent != NULL) {
        printf("Person %d\n", index);
        printf("name: %s\n", pCurrent->pPerson->name);
        printf("id: %s\n", pCurrent->pPerson->id);
        index += 1;
        pCurrent = pCurrent->pNext;
    }
}

【问题讨论】:

  • 提示:不要让结构体的字符串被曲解为MAX,而是使用大小完全符合需要的分配,如char* 属性。
  • 提示:不要将X* x = NULL; x = (X*) malloc(sizeof(X))分两步,简化为X* x = malloc(sizeof(X))即可。无需演员表。没有pre-NULL。
  • ??????小心前行,看看会发生什么?
  • 查看我的回答的最后更新:stackoverflow.com/a/67721142/4386427

标签: c pointers struct singly-linked-list


【解决方案1】:

您的insert 函数有几个错误。

简短的回答是:

Node* insert(Node* pHead, Node* pNewNode) {
    Node* pCurrent = pHead;
    if (pCurrent == NULL)
    {
        return pNewNode;
    }
    while (pCurrent->next != NULL) {
        pCurrent = pCurrent->pNext;
    }
    pCurrent->pNext = pNewNode;
    return pHead;
}

然后这样称呼它:

Node* pHead = NULL;
Person* pPerson = newPerson();
Node* pNewNode = newNode(pPerson);
pHead = insert(pHead, pNewNode);

并且还在newNode函数中插入pPeople->pNext = NULL;

解释:

从这里开始:

void insert(Node* pHead, Person* pNewPerson) {

为什么要用pNewPerson 作为参数?您已经使用 newNode 函数将新人员插入到节点中。所以改为:

void insert(Node* pHead, Node* pNewNode) {
    ...
}

然后这样称呼它:

Node* pHead = NULL;
Person* pPerson = newPerson();
Node* pNewNode = newNode(pPerson);
insert(pHead, pNewNode);

这部分:

while (pCurrent != NULL) {
    pCurrent = pCurrent->pNext;
}

将您带到最后一个元素,因此您无法将新元素添加到最后一个元素。你的代码应该是:

void insert(Node* pHead, Node* pNewNode) {
    Node* pCurrent = pHead;
    if (pCurrent == NULL)
    {
        ..se later..
    }
    while (pCurrent->next != NULL) {
        pCurrent = pCurrent->pNext;
    }
    // Now pCurrent points to the last element

您插入元素的代码也是错误的 - 请参阅 cmets

pCurrent = (Node*)malloc(sizeof(Node));  // Why malloc - you did that in newNode-function
pCurrent->pNext->pPerson = pNewPerson;   // Why - you did that in newNode-function
pCurrent->pNext = NULL;

大部分代码都不需要。做吧:

void insert(Node* pHead, Node* pNewNode) {
    Node* pCurrent = pHead;
    if (pCurrent == NULL)
    {
        ..se later..
    }
    while (pCurrent->next != NULL) {
        pCurrent = pCurrent->pNext;
    }
    pCurrent->pNext = pNewNode;
}

现在如果 pHead 是 NULL 怎么办?在这种情况下,您需要将pHead 更新为新节点。为此,您需要再次更改该功能。一种方法是:

Node* insert(Node* pHead, Node* pNewNode) {
    Node* pCurrent = pHead;
    if (pCurrent == NULL)
    {
        return pNewNode;
    }
    while (pCurrent->next != NULL) {
        pCurrent = pCurrent->pNext;
    }
    pCurrent->pNext = pNewNode;
    return pHead;
}

然后这样称呼它:

Node* pHead = NULL;
Person* pPerson = newPerson();
Node* pNewNode = newNode(pPerson);
pHead = insert(pHead, pNewNode);

【讨论】:

  • 你的解释很清楚!感谢您找出我仍然没有意识到的事情!
【解决方案2】:

在您的insert 函数中,您访问pCurrent->pNext,但是,pNext 未由newNode 初始化。

【讨论】:

    【解决方案3】:

    你可以像下面这样改变插入函数的while循环。

       while (pCurrent != NULL && pCurrent->pNext != NULL) {
            pCurrent = pCurrent->pNext;
        }
     
    

    出现分段错误错误的原因是当 pCurrent->pNext 为 Null 时将 pCurrent 指针设置为空值。之后,您尝试在以下代码中取消引用空指针;

    pCurrent->pNext = malloc(sizeof(struct Node));
    

    您也可以查看@4386427 的答案和解释我没有查看您的代码的其他部分,但是由于我上面指定的原因,您收到了分段错误。

    【讨论】:

      猜你喜欢
      • 2021-04-26
      • 1970-01-01
      • 2018-12-25
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      相关资源
      最近更新 更多