最近,我通过编写不同的数据结构来提高编程技能,而这正是开始!链接列表操作(核心转储)
现在我正在写链表,但有些恼人的事情发生了,麻烦已经很长时间让我很烦恼,因为我对这个错误不太确定, 分段错误(核心转储),但我确实知道我在内存操作上做错了。
link_list.h:
struct LINK_LIST {
char *string;
struct LINK_LIST *next;
}link_list;
==============================
link_list .C:
#include<stdio.h>
#include<stdlib.h>
int init_link_list(struct LINK_LIST *new_link) {
//char *new_string;
int i;
//new_string = (char *)malloc(sizeof(char) * STRING_SIZE);
new_link = (struct LINK_LIST *)malloc(sizeof(struct LINK_LIST));
if (new_link==NULL) {
fprintf(stderr, "Insufficient memory!!!");
return ERROR;
}
//new_link->string = new_string;
new_link->string = NULL;
//new_link->next = NULL;
return OK;
}
在这里,我自己定义的init操作,那么插入操作:
int insert(struct LINK_LIST *link, int pos, char *in) {
int i;
if (get_length(link)>=STRING_SIZE) {
fprintf(stderr, "Link list is full!!!");
return ERROR;
}
else {
if (pos < 0 || pos-1 > get_length(link)) {
fprintf(stderr, "Invalid position");
return ERROR;
}
else {
i = 0;
do {
struct LINK_LIST *new_node;
init_link_list(new_node);
new_node->next = link->next;
link->next = new_node;
new_node->string = in;
i += 1;
} while(i<pos-1);
}
}
return OK;
}
您的第一份工作是删除在'='右侧的'(struct LINK_LIST *)'投射。 – Bathsheba
您需要遵循以下准则:http://sscce.org/发布您可以制作的最小的失败测试用例。 –
'init_link_list(new_node);'之后,指针'new_node'仍未初始化。 – aschepler