问题在C中初始化Double Stacked链表

时间:2015-11-05 05:51:07

标签: c

我正在编写一个涉及一组两个链表的数据结构,这些链表彼此叠加。当我尝试在我的测试工具中初始化该集时,我收到了分段错误。我已经评论了所有价值制定者进行测试,看看我是否能够自己找出错误,但我做不到。

init方法的原型:

测试工具:

int
main( )
{
  list the_list;
  int used = 0;
  int values[MAX_VALUES];
  char input[LINE_LEN];
  char command;
  int argument;
  int num_found;
  bool result;
  set_t lower;
  set_t upper;
  the_list->lower = lower;
  the_list->upper = upper;

 input[0] = '\0';
  input[LINE_LEN-1] = '\0';

  fgets( input, LINE_LEN, stdin );
  while (*input != 'q') {
    num_found = sscanf( input, "%c %d", &command, &argument );

    if (num_found > 0) {
      switch (command) {
        case 'i':
          printf ("Request to initialize the set\n");
          if (num_found == 1) {
            result = set_init( &the_list );
          } else {
            result = set_init( NULL );
          }
          printf ("Returned as %d\n", result);
          break;


                                                        34,0-1         8%

Init方法:

bool
set_init( list *the_list )
{
  bool initialized = false;
  if (the_list !=NULL ) {
    /* We have space to initialize. */

    the_list->lower->set_size = 0;
   /* the_list->lower->head = NULL;
    the_list->lower->tail = NULL;
    the_list->lower->set_level = 1;
    the_list->lower->ready = true;
    the_list->upper->set_size = 0;
    the_list->upper->head = NULL;
    the_list->upper->tail = NULL;
    the_list->upper->set_level = 2;
    the_list->upper->ready = true;*/
    initialized = true;
    }
  return initialized;
  }

我的set,链表和节点结构的结构定义:

typedef struct _set_node_t {
        test_type_t *data;
        struct _set_node_t *next;
        struct _set_node_t *below;
} set_node_t;

/* the set itself keeps track of the head and the tail of the linked list */
typedef struct {

        int set_size;
        bool ready;
        set_node_t *head;
        set_node_t *tail;
        int set_level;
} set_t;

typedef struct {
        set_t *lower;
        set_t *upper;
}list;

1 个答案:

答案 0 :(得分:0)

唯一可能崩溃的是这一行:

the_list->lower->set_size = 0;

the_list或the_list-> lower必须是未初始化的或NULL或指向无效或无法访问的内存。

编辑:是的,因为你没有初始化the_list.lower,这一行会崩溃:

result = set_init( &the_list );

这条线会崩溃,因为你传递的是NULL:

result = set_init( NULL );