在单链表中存储信息结构
store struct of info in single linked list
我正在尝试将人员信息结构存储到单个链表中并对其执行一些操作
我的结构信息是
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
函数,它在列表中存储 struct 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;
}
}
在您的 insert
函数中,您访问 pCurrent->pNext
,但是 pNext
未由 newNode
初始化。
您的 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
您插入元素的代码也有误 - 请参阅评论
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);
你可以像下面这样改变插入函数的while循环。
while (pCurrent != NULL && pCurrent->pNext != NULL) {
pCurrent = pCurrent->pNext;
}
您收到分段错误的原因是当 pCurrent->pNext 为 Null 时,您将空值设置为 pCurrent 指针。之后,您尝试在以下代码中取消引用空指针;
pCurrent->pNext = malloc(sizeof(struct Node));
您还可以查看@4386427 的回答和解释,我没有查看您代码的其他部分,但由于我在上面指定的原因,您收到了分段错误。
我正在尝试将人员信息结构存储到单个链表中并对其执行一些操作
我的结构信息是
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
函数,它在列表中存储 struct 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;
}
}
在您的 insert
函数中,您访问 pCurrent->pNext
,但是 pNext
未由 newNode
初始化。
您的 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
您插入元素的代码也有误 - 请参阅评论
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);
你可以像下面这样改变插入函数的while循环。
while (pCurrent != NULL && pCurrent->pNext != NULL) {
pCurrent = pCurrent->pNext;
}
您收到分段错误的原因是当 pCurrent->pNext 为 Null 时,您将空值设置为 pCurrent 指针。之后,您尝试在以下代码中取消引用空指针;
pCurrent->pNext = malloc(sizeof(struct Node));
您还可以查看@4386427 的回答和解释,我没有查看您代码的其他部分,但由于我在上面指定的原因,您收到了分段错误。