为什么这个程序会导致段错误?

发布于 2024-09-18 05:07:23 字数 3892 浏览 3 评论 0原文

大家好,我是新人,所以我相信你们会有所帮助 我在跳过列表方面遇到一些问题,这是

#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;
     }

我编译得很好的代码,但是当我运行执行时它突然停止工作,请帮助我

hi guys i am new so i am sure you will help
i have some trouble with skip list here is code

#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;
     }

i compiles fine but when i run for execute it stops working suddenly please help me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘味果▽酱 2024-09-25 05:07:23

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

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

这显然是错误的。当 x 为 NULL 时,您试图取消引用它。将其更改为

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

When I run your code in gdb, I see a crash in your insert method:

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

which is clearly wrong. When x is NULL you are trying to dereference it. Change it to

if ( x!=NULL && x->value!=value){
好久不见√ 2024-09-25 05:07:23

这里的这一行:

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

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

This line here:

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

in the insert function - shouldn't it be x != NULL - assuming you want a short-circuit safety test. This is where it's blowing up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文