我再问一个问题。很抱歉打扰了所有人。
实际上,我破坏了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);
之后的第三个节点是垃圾。
首先我认为这可能是我第一次编写伪代码的逻辑问题。所以我查了一下,但很清楚。
第二次我认为这可能是我的电脑问题,但绝对不是。
最后我认为这可能是一个节点计数过程&#39;问题,但它工作得很清楚。
我应该如何做这项工作?
答案 0 :(得分:1)
您没有将item.data与new_item-&gt;数据等同起来:
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;
}
在您提问之前,请考虑下次使用调试器。 希望它有所帮助。