c-附加ADT列表seg错误



所以我试图附加一个列表,但每次使用附加函数时,它都会崩溃(SEG FAULT(。我把它缩小到插入值的一行附近,并在下面的代码中标记了它。

这是结构/列表

/*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的工作是添加值以损坏前端find_prev_gt的工作是找到先前最大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;
}

每次我使用append函数时,它只会使崩溃

您的问题出现在函数push中,其中节点的值在初始化为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中,您的代码更改了headtail,但没有更改head_sorttail_sort,但在节点设置了sort而没有设置next

相关内容

  • 没有找到相关文章

最新更新