gpt4 book ai didi

c - C中的简单链表(2): Trash result

转载 作者:行者123 更新时间:2023-11-30 19:07:37 25 4
gpt4 key购买 nike

我再问一个问题。非常抱歉打扰大家。

事实上,我破解了 Memory Access Problem 。但我无法理解执行的结果。

来源就在这里。

/*
https://stackoverflow.com/questions/46602732/simple-linked-list-in-c-memory-access-error
*/

#include "List.h" //main.h and stdlib.h


typedef int element;
typedef struct _ListNode //define the form of Node
{
element data;
struct _ListNode *link;
} ListNode;


ListNode *header = NULL; //header node is global
int num_node = 0; //node-counting

//define function
int AppendNode(const ListNode item);
void DisplayItem(void);
int InsertNode(const int pos, const ListNode item);


int AppendNode(const ListNode item)
{
ListNode *current, *new_item = NULL; //'current' for searching. 'new_item' for Appending
new_item = (ListNode *)malloc(sizeof(ListNode));

if (new_item == NULL) return 0; //fail for Dynamic Allocation

new_item->data = item.data;
new_item->link = NULL;

if (header == NULL)
header = new_item;
else{
current = header;
while (current->link != NULL)
current = current->link;
current->link = new_item; //BUSTED!
}
num_node++;
return 1;
}


void DisplayItem(void)
{
ListNode *current = NULL;
current = header;
while (current != NULL)
{
printf("%d\n", current->data);
current = current->link;
}
}



int InsertNode(const int position, const ListNode item)
{
ListNode *current = NULL;
ListNode *new_item = NULL;
new_item = (ListNode *)malloc(sizeof(ListNode));

if (new_item == NULL) return 0;

if (position == 1)
{
new_item->link = header;
header = new_item;
num_node++;
}
else if((1 < position) && (position <= (num_node+1)))
{
int current_position = 0;
current = header;
while (current_position != (position - 2))
{
current = current->link;
current_position++;
}
new_item->link = current->link;
current->link = new_item;
num_node++;
}
else return 0;
}


int main(void)
{
ListNode node1; node1.data = 10;
ListNode node2; node2.data = 20;
ListNode node3; node3.data = 40;
ListNode node4; node4.data = 50;
ListNode node5; node5.data = 60;

AppendNode(node1);
AppendNode(node2);
AppendNode(node3);
AppendNode(node4);
AppendNode(node5);

DisplayItem();

printf("========================\n");

ListNode insert; insert.data = 30;
InsertNode(3,insert);
DisplayItem();

getchar();
return 0;
}

该来源的结果是

10
20
40
50
60
========================
10
20
8476480
40
50
60

InsertNode(3,insert); 之后的第三个节点是垃圾。

首先我认为这可能是一个逻辑问题,我首先编写了伪代码。所以我查了一下,但很清楚。

第二我以为可能是我的电脑问题,但绝对不是。

最后我认为这可能是节点计数过程的问题,但它工作得很清楚。

我应该如何完成这项工作?

最佳答案

您没有将 item.data 等同于 new_item->data :

int InsertNode(const int position, const ListNode item)
{
ListNode *current = NULL;
ListNode *new_item = NULL;
new_item = (ListNode *)malloc(sizeof(ListNode));
current = (ListNode *)malloc(sizeof(ListNode));
new_item->data = item.data; //This line is not present in your code

if (new_item == NULL) return 0;

if (position == 1)
{
new_item->link = header;
header = new_item;
num_node++;
}
else if((1 < position) && (position <= (num_node+1)))
{
int current_position = 0;
current = header;
while (current_position != (position - 2))
{
current = current->link;
current_position++;
}
new_item->link = current->link;
current->link = new_item;
num_node++;
}
else return 0;
}

下次提问之前请考虑使用调试器。希望对您有所帮助。

关于c - C中的简单链表(2): Trash result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46610844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com