Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么该程序会导致SEG故障?_C_Data Structures_Segmentation Fault_Skip Lists - Fatal编程技术网

C 为什么该程序会导致SEG故障?

C 为什么该程序会导致SEG故障?,c,data-structures,segmentation-fault,skip-lists,C,Data Structures,Segmentation Fault,Skip Lists,大家好,我是新来的,所以我相信你们会帮我的 我有一些问题,跳过列表这里是代码 #include <stdio.h> #include<stdlib.h> #include<time.h> #include <string.h> #define P 0.5 #define MAX_LEVEL 6 struct sn{ int value; struct sn **forward; }; typ

大家好,我是新来的,所以我相信你们会帮我的 我有一些问题,跳过列表这里是代码

#include <stdio.h>
#include<stdlib.h>
#include<time.h>
#include <string.h>
#define P 0.5
#define MAX_LEVEL 6
 struct sn{
          int value;
        struct sn **forward;
         };
 typedef struct sn skipnode;
 typedef struct{
     skipnode * header;
      int level;
      }skipset;
 float frand(){

     return (float) rand()/RAND_MAX;

 }
 int random_level(){
     static int first=1;
     int lvl=0;
        if (first) {
             srand((unsigned) time(NULL));
             first=0;
        }
          while (frand()<P && lvl <MAX_LEVEL)
               lvl++;
          return lvl;
 }
 skipnode* make_node( int level,int value){
      skipnode *sn=(skipnode*)malloc(sizeof(skipnode));
      sn->forward=(skipnode**) calloc(level+1,sizeof(skipnode));
      sn->value=value;
      return sn;
 }


 skipset *makeskipset(){
      skipset *ss=(skipset*)malloc(sizeof(skipset));
      ss->header=make_node(MAX_LEVEL,0);
      ss->level=0;
       return ss;

 }
 void print_skipset(skipset *ss){

     skipnode *x=ss->header->forward[0];
     printf("(");
      while (x!=NULL){
          printf("%d",x->value);
          x=x->forward[0];
            if (x!=NULL)
                printf(",");



      }
      printf("}\n");

 }
 int contains(skipset *ss,int search_value){
      int i;
      skipnode *x=ss->header;
      for (i=ss->level;i>=0;i--){
          while (x->forward[i]!=NULL && x->forward[i]->value<search_value){

              x=x->forward[i];
          }
          }
      x=x->forward[0];
      if (x!=NULL && x->value==search_value)
           return 1;
       return 0;
 }

 void insert(skipset *ss,int value){
     int i;
     skipnode *x=ss->header;
     skipnode* update[MAX_LEVEL+1];
      memset (update,0,MAX_LEVEL+1);
      for (i=ss->level;i>=0;i--){
          while ( x->forward[i]!=NULL && x->forward[i]->value<value){
              x=x->forward[i];
          }

          update[i]=x;


      }
      x=x->forward[0];
      if ( x==NULL && x->value!=value){
          int lvl=random_level();
          if (lvl>ss->level){
              for (i=ss->level+1;i<=lvl;i++){
                  update[i]=ss->header;
          }
              ss->level=lvl;

      }
          x=make_node(lvl,value);
            for (i=0;i<=lvl;i++){
                x->forward[i]=update[i]->forward[i];
                update[i]->forward[i]=x;

            }

 }
 }

 void Delete( skipset *ss,int value){

     int i;
     skipnode *x=ss->header;
     skipnode *update[MAX_LEVEL+1];
     memset(update,0,MAX_LEVEL+1);
     for (i=ss->level;i>=0;i--){

         while (x->forward[i] !=NULL && x->forward[i]->value<value){
             x=x->forward[i];
         }
         update[i]=x;
     }
     x=x->forward[0];
     if (x->value==value){

         for (i=0;i<ss->level;i++){
             if (update[i]->forward[i]!=x)
                  break;
             update[i]->forward[i]=x->forward[i];
         }
          free(x);
          while (ss->level>0 &&ss->header->forward[ss->level]==NULL){
              ss->level--;

     }
 }
 }
     int main(){

          skipset *ss=makeskipset();
           print_skipset(ss);

            insert(ss,3);
            insert(ss,10);
            insert(ss,7);
            insert(ss,11);
            insert(ss,20);
            insert(ss,34);
              if (contains(ss,7)){
                  printf(" 7 is in the list\n");
              }
              print_skipset(ss);
              Delete(ss,7);
              print_skipset(ss);
              return 0;
     }
我编译得很好,但当我运行for execute时,它突然停止工作请帮助我

当我在gdb中运行您的代码时,我看到您的insert方法崩溃:

这显然是错误的。当x为NULL时,您正试图取消对它的引用。换成

if ( x!=NULL && x->value!=value){
当我在gdb中运行您的代码时,我看到您的insert方法发生崩溃:

这显然是错误的。当x为NULL时,您正试图取消对它的引用。换成

if ( x!=NULL && x->value!=value){
这一行在这里:

if ( x==NULL && x->value!=value){ 
在插入函数中-不应该是x!=空-假设您需要短路安全测试。这就是爆炸的地方。

这一行:

if ( x==NULL && x->value!=value){ 

在插入函数中-不应该是x!=空-假设您需要短路安全测试。这就是它爆炸的地方。

祝你好运,有人能看完这一切。我的建议是:温习一下指针。它们是导致运行时错误、异常和超出内存范围警告的主要原因。如果不想使用调试器,请在代码中随意添加fprintfstderr、OK(到目前为止)%d\n、\uuuuu LINE\uuu;找出问题所在。希望有人能通读这些内容。我的建议是:温习一下指针。它们是导致运行时错误、异常和超出内存范围警告的主要原因。如果不想使用调试器,请在代码中随意添加fprintfstderr、OK(到目前为止)%d\n、\uuuuu LINE\uuu;确定问题所在。如果x==NULL | | x->value!=值?如果x==NULL | | x->value!=价值