附加 ADT 列表段错误
Appending a ADT list seg fault
所以我试图附加一个列表,但每次我使用附加功能时,它都会崩溃(SEG FAULT)。我将其缩小到插入值中的一行,并在下面的代码中对其进行了标记。
所以这是 structs/lists
/*Declration of the struct*/
typedef struct Element {
value_type value;
Key_type key;
struct Element * next;
struct Element * sort;
}Node;
/*ADT declration*/
typedef struct List {
Node * head;
Node * head_sort;
Node * tail;
Node * tail_sort;
int size;
}Sorted_List;
这是剩余的代码
/*Fucntion to add values ot the end using tail*/
int append_list (Sorted_List * List, value_type value, Key_type key){
int result = insert_value(List->tail, value, key) != NULL;
if ( result == SUCCESS){
List->size++;
List->tail = List->tail->sort;
}
return result;
}
int append (Sorted_List * List, value_type value, Key_type key){
return is_empty(List) ? push(List, value, key)
: append_list(List, value, key);
}
/*Function to insert value into list*/
Node * insert_value (Node * node, value_type value, Key_type key){
/*Setting a new node and mallocing it */
Node * new_node = malloc(sizeof(Node));
/*Checking for the new node to not equal null*/
if (new_node != NULL){
/*Setting the values for it*/
new_node->value = value;
new_node->key = key;
/*Setting the new node next to equal old nodes next*/
new_node->sort = node->sort;
/*Setting old node next to equal new node*/
node->sort = new_node;
/////I receive the error around this line^^^^/////
}
return new_node;
}
编辑::
所以这是程序中使用的推送功能的代码
add front 的工作是添加 rot the front 的值
而find_prev_gt的工作是找到previes最大valus的位置来添加新的数字
/*Function to push a value to to the front of the list*/
int push (Sorted_List * List, value_type value, Key_type key) {
Node * node = NULL;
int empty = 0;
empty = is_empty(List);
Node * next_node = NULL;
Node * insert_node = find_prev_gt(List->head, key);
int result = FAILURE;
if (insert_node == NULL) {
add_front(&(List->head), value,key);
}
else {
next_node = insert_node->sort;
if (next_node == NULL || next_node->key != key)
insert_value(insert_node, value,key);
}
result = (node != NULL );
/*Returns success if reseult is succesfull*/
if ( result == SUCCESS) {
List->size++;
if (empty)
List->tail = List->head;
}
return result;
}
Node * add_front(Node ** head, value_type value, Key_type key){
Node *new_node = malloc(sizeof(Node));
if (new_node != NULL) {
new_node->key = key;
new_node->value = value;
new_node->sort = *head;
*head = new_node; }
return new_node;
}
/*Function to check if list is empty*/
int is_empty (Sorted_List * list){
return list->head == NULL;
}
Node * find_prev_gt ( Node * head, Key_type key ) {
Node * node = head, * prev = NULL;
while (node != NULL && node->key < key){
prev = node;
node = node->sort;
}
return prev;
}
every time I use the append function, it just crashes
你的问题出在函数 push 中 node 的值在初始化为 NULL 后没有改变,所以在
if ( result == SUCCESS) {
List->size++;
if (empty)
List->tail = List->head;
}
测试始终为假,相关代码未执行
你只需要改变两行,替换行
add_front(&(List->head), value,key);
来自
node = add_front(&(List->head), value,key);
和行
insert_value(insert_node, value,key);
来自
node = insert_value(insert_node, value,key);
如果我进行这两项更改并在定义前添加您的代码
#include <stdlib.h>
#include <stdio.h>
typedef int value_type;
typedef int Key_type;
#define SUCCESS 1
#define FAILURE 0
并添加以下 main :
int main()
{
Sorted_List list = { 0, 0, 0, 0, 0 };
Node * node;
append(&list, 2, 22);
append(&list, 3, 33);
append(&list, 1, 11);
for (node = list.head; node != NULL; node = node->sort)
printf("[key=%d, value=%d] ", node->key, node->value);
putchar('\n');
return 0;
}
执行写入:
[key=22, value=2] [key=33, value=3] [key=11, value=1]
在 Sorted_List 之外,您的代码更改 head 和 tail但不是 head_sort 也不是 tail_sort 但在 Node 你设置 sort 而不是next,这不符合逻辑,而且节点没有排序
所以我试图附加一个列表,但每次我使用附加功能时,它都会崩溃(SEG FAULT)。我将其缩小到插入值中的一行,并在下面的代码中对其进行了标记。
所以这是 structs/lists
/*Declration of the struct*/
typedef struct Element {
value_type value;
Key_type key;
struct Element * next;
struct Element * sort;
}Node;
/*ADT declration*/
typedef struct List {
Node * head;
Node * head_sort;
Node * tail;
Node * tail_sort;
int size;
}Sorted_List;
这是剩余的代码
/*Fucntion to add values ot the end using tail*/
int append_list (Sorted_List * List, value_type value, Key_type key){
int result = insert_value(List->tail, value, key) != NULL;
if ( result == SUCCESS){
List->size++;
List->tail = List->tail->sort;
}
return result;
}
int append (Sorted_List * List, value_type value, Key_type key){
return is_empty(List) ? push(List, value, key)
: append_list(List, value, key);
}
/*Function to insert value into list*/
Node * insert_value (Node * node, value_type value, Key_type key){
/*Setting a new node and mallocing it */
Node * new_node = malloc(sizeof(Node));
/*Checking for the new node to not equal null*/
if (new_node != NULL){
/*Setting the values for it*/
new_node->value = value;
new_node->key = key;
/*Setting the new node next to equal old nodes next*/
new_node->sort = node->sort;
/*Setting old node next to equal new node*/
node->sort = new_node;
/////I receive the error around this line^^^^/////
}
return new_node;
}
编辑:: 所以这是程序中使用的推送功能的代码 add front 的工作是添加 rot the front 的值 而find_prev_gt的工作是找到previes最大valus的位置来添加新的数字
/*Function to push a value to to the front of the list*/
int push (Sorted_List * List, value_type value, Key_type key) {
Node * node = NULL;
int empty = 0;
empty = is_empty(List);
Node * next_node = NULL;
Node * insert_node = find_prev_gt(List->head, key);
int result = FAILURE;
if (insert_node == NULL) {
add_front(&(List->head), value,key);
}
else {
next_node = insert_node->sort;
if (next_node == NULL || next_node->key != key)
insert_value(insert_node, value,key);
}
result = (node != NULL );
/*Returns success if reseult is succesfull*/
if ( result == SUCCESS) {
List->size++;
if (empty)
List->tail = List->head;
}
return result;
}
Node * add_front(Node ** head, value_type value, Key_type key){
Node *new_node = malloc(sizeof(Node));
if (new_node != NULL) {
new_node->key = key;
new_node->value = value;
new_node->sort = *head;
*head = new_node; }
return new_node;
}
/*Function to check if list is empty*/
int is_empty (Sorted_List * list){
return list->head == NULL;
}
Node * find_prev_gt ( Node * head, Key_type key ) {
Node * node = head, * prev = NULL;
while (node != NULL && node->key < key){
prev = node;
node = node->sort;
}
return prev;
}
every time I use the append function, it just crashes
你的问题出在函数 push 中 node 的值在初始化为 NULL 后没有改变,所以在
if ( result == SUCCESS) { List->size++; if (empty) List->tail = List->head; }
测试始终为假,相关代码未执行
你只需要改变两行,替换行
add_front(&(List->head), value,key);
来自
node = add_front(&(List->head), value,key);
和行
insert_value(insert_node, value,key);
来自
node = insert_value(insert_node, value,key);
如果我进行这两项更改并在定义前添加您的代码
#include <stdlib.h>
#include <stdio.h>
typedef int value_type;
typedef int Key_type;
#define SUCCESS 1
#define FAILURE 0
并添加以下 main :
int main()
{
Sorted_List list = { 0, 0, 0, 0, 0 };
Node * node;
append(&list, 2, 22);
append(&list, 3, 33);
append(&list, 1, 11);
for (node = list.head; node != NULL; node = node->sort)
printf("[key=%d, value=%d] ", node->key, node->value);
putchar('\n');
return 0;
}
执行写入:
[key=22, value=2] [key=33, value=3] [key=11, value=1]
在 Sorted_List 之外,您的代码更改 head 和 tail但不是 head_sort 也不是 tail_sort 但在 Node 你设置 sort 而不是next,这不符合逻辑,而且节点没有排序