繁体   English   中英

附加 ADT 列表段错误

[英]Appending a ADT list seg fault

所以我试图 append 一个列表,但每次我使用 append function,它只是崩溃(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;

这是代码的rest

/*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;
}

编辑:: 所以这里是程序中使用的推送 function 的代码 add front 的工作是添加值 rot the front 而 find_prev_gt 的工作是找到 previes 最大值的位置以添加新数字

/*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 function 时,它都会崩溃

您的问题出在 function推送中,其中节点的值在初始化为 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

并添加以下主要内容:

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但在Node中您设置sort但不是next ,这不合逻辑,此外节点未排序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM