C 跳过不在更高级别插入元素的列表

C 跳过不在更高级别插入元素的列表,c,skip-lists,C,Skip Lists,我在跳过列表中插入时遇到问题。 当我将元素插入跳过列表时,它只将元素添加到较低级别,而不是较高级别。 当我试图进入下层时,它会爆炸。 谢谢你的帮助。 多谢各位 #ifndef _SkipList_ #define _SkipList_ // a value not supposed in stack and queue #define EMPTY 0x7FFFFFFF // new types typedef int DATA; // a ty

我在跳过列表中插入时遇到问题。 当我将元素插入跳过列表时,它只将元素添加到较低级别,而不是较高级别。 当我试图进入下层时,它会爆炸。 谢谢你的帮助。 多谢各位

#ifndef _SkipList_
#define _SkipList_

// a value not supposed in stack and queue
#define     EMPTY   0x7FFFFFFF

// new types
typedef int DATA;                   // a type for data (easy to change)
typedef enum {False, True} BOOL;    // a boolean type

// Node
typedef struct node 
{
    DATA            key;
    struct node*    next;
    struct node*    down;

}NODE;

typedef struct 
{
    NODE *head;
}SkipList;

#endif

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "SkipList.h"

BOOL SL_init(SkipList *list)
{
    if (!list)
        return False;
    NODE *temp = (NODE*)malloc(sizeof(NODE));
    temp->next = (NODE*)malloc(sizeof(NODE));

    temp->down = NULL;
    temp->key = INT_MIN;
    temp->next->key = INT_MAX;
    list->head = (NODE*)malloc(sizeof(NODE));
    list->head = temp;

    NODE *start, *end;
    start = (NODE*)malloc(sizeof(NODE));
    end = (NODE*)malloc(sizeof(NODE));
    start->key = INT_MIN;
    start->down = NULL;
    end->down = NULL;
    end->key = INT_MAX;
    list->head = start;
    list->head->next = end;
    return True;

}

NODE* SL_find(SkipList *list, DATA Value)
{
    NODE *p = list->head;
    while(1)
    {
        for(;p->next->key <= Value; p = p->next);
        if(p->down == NULL)
            break;
        p = p->down;
    }
    if(p->key==Value)
        return p;
    else 
        return NULL;
}
NODE* new_node(DATA x, NODE *n, NODE *d){
    NODE *p;
    p = (NODE*)malloc(sizeof(NODE));
    p->key = x;
    p->next = n;
    p->down = d;
    return p;
}
BOOL toss()
{
    int random = rand() % 2;
    printf(" %d\n", random);
    if (random == 0){
        return False;
    }
    return True;
}

NODE *SkipList_addAfter(NODE*p, DATA Value)
{
    NODE *temp = (NODE*)malloc(sizeof(NODE));
    if (temp)
    {
        temp->key = Value;
        temp->next = p->next;
        temp->down = p->next->down;
        p->next = temp;
    }
    return temp;
}



NODE* insert2(NODE *old_lst, DATA x){
    NODE *deeper, *newnode;
    for (; old_lst->next->key <= x ; old_lst = old_lst->next);
    NODE *temp ;
    if (old_lst->down == NULL){
        temp = SkipList_addAfter(old_lst, x);
        return temp;
    }
    deeper = insert2(old_lst->down, x);
    if (deeper == NULL || toss())
        return NULL;
    else{
        newnode = SkipList_addAfter(old_lst, x);
        newnode->down = deeper;
    }
    return newnode;

}
NODE* insert1(NODE *lst, DATA x){
    NODE *deeper;
    deeper = insert2(lst,x);
    if (deeper == NULL || toss())
        return lst;
    return new_node(INT_MIN, INT_MAX, lst);
}


NODE* insert(SkipList *list, DATA x){
    if (SL_find(list, x))
        return NULL;    
    return insert1(list->head, x);
}
void print(SkipList *list){
    NODE *temp = list->head->next;
//  while (!temp->down){
        for (; temp->key <INT_MAX; temp = temp->next){
            printf("->%d\n", temp->key);
//      }
//      temp = temp->down;
//      printf("\n\n");
    }
}
void deleteD(NODE *node)
{
    NODE* tmp;

    if (!node || !(tmp = node->next)) 
        return False;


    node->next = tmp->next;
    free(tmp);
    return True;

}
BOOL SL_Delete(SkipList *list, DATA x)
{
    NODE *p = SL_find(list, x);
    if (p == NULL)
        return False;
    NODE *temp = list->head;
    for (; temp->next->key < p->key; temp = temp->next);
    while (temp->down != NULL){
        deleteD(temp);
        temp = temp->down;
    }
    deleteD(temp);
    return True;

}
void main(){
    SkipList *list = (SkipList*)malloc(sizeof(SkipList));
    SL_init(list);

    int arr[6] = { 1, 2, 3, 4, 5,5};
    int i;
    for (i = 0; i < 6; i++){
        insert(list, arr[i]);
    }
    /*
    if (!SL_Delete(list, 7))
        printf("cant delete");
        */
    print(list);
//  SL_Delete(list, 4);
//  print(list);
//  if (SL_find(list, 7))
//      printf("\nfound");

}
\ifndef\u SkipList_
#定义_SkipList_
//堆栈和队列中不应包含的值
#定义空的0x7FFFFFFF
//新型
typedef int DATA;//数据类型(易于更改)
typedef枚举{False,True}BOOL;//布尔型
//节点
类型定义结构节点
{
数据键;
结构节点*下一步;
结构节点*向下;
}节点;
类型定义结构
{
节点*头;
}技工;
#恩迪夫
#包括
#包括
#包括
#包括“SkipList.h”
BOOL SL_init(SkipList*列表)
{
如果(!列表)
返回False;
NODE*temp=(NODE*)malloc(sizeof(NODE));
temp->next=(NODE*)malloc(sizeof(NODE));
temp->down=NULL;
temp->key=INT\u MIN;
temp->next->key=INT\u MAX;
列表->头=(节点*)malloc(节点大小);
列表->头部=温度;
节点*开始,*结束;
start=(NODE*)malloc(sizeof(NODE));
end=(NODE*)malloc(sizeof(NODE));
开始->键=INT\u MIN;
开始->下降=空;
结束->向下=空;
结束->键=INT_MAX;
列表->头部=开始;
列表->标题->下一步=结束;
返回True;
}
节点*SL_查找(SkipList*列表,数据值)
{
节点*p=列表->头部;
而(1)
{
对于(;p->next->key next);
如果(p->down==NULL)
打破
p=p->down;
}
如果(p->key==值)
返回p;
其他的
返回NULL;
}
节点*新节点(数据x,节点*n,节点*d){
节点*p;
p=(NODE*)malloc(sizeof(NODE));
p->key=x;
p->next=n;
p->down=d;
返回p;
}
掷骰子
{
int random=rand()%2;
printf(“%d\n”,随机);
如果(随机==0){
返回False;
}
返回True;
}
节点*SkipList\u addAfter(节点*p,数据值)
{
NODE*temp=(NODE*)malloc(sizeof(NODE));
如果(临时)
{
温度->键=值;
温度->下一步=p->下一步;
温度->下降=p->下一步->下降;
p->next=温度;
}
返回温度;
}
节点*insert2(节点*old_lst,数据x){
节点*更深,*新节点;
用于(;旧的上一步->下一步->下一步键);
节点*温度;
如果(旧的->向下==NULL){
温度=SkipList\u addAfter(旧的,x);
返回温度;
}
更深=插入2(旧底->向下,x);
if(deep==NULL | | toss())
返回NULL;
否则{
newnode=SkipList\u addAfter(old\u lst,x);
新建节点->向下=更深;
}
返回newnode;
}
节点*insert1(节点*lst,数据x){
节点*更深;
更深=插入2(lst,x);
if(deep==NULL | | toss())
返回lst;
返回新的_节点(INT_MIN、INT_MAX、lst);
}
节点*插入(SkipList*列表,数据x){
if(SL_查找(列表,x))
返回NULL;
返回insert1(列表->头部,x);
}
作废打印(SkipList*列表){
节点*temp=list->head->next;
//同时(!temp->down){
对于(;临时->键下一步){
printf(“->%d\n”,临时->键);
//      }
//温度=温度->下降;
//printf(“\n\n”);
}
}
已删除无效(节点*节点)
{
节点*tmp;
如果(!node | |!(tmp=node->next))
返回False;
节点->下一步=tmp->下一步;
免费(tmp);
返回True;
}
BOOL SL_Delete(SkipList*列表,数据x)
{
节点*p=SL_查找(列表,x);
if(p==NULL)
返回False;
节点*temp=list->head;
用于(;temp->next->keykey;temp=temp->next);
while(临时->停机!=NULL){
删除(临时);
温度=温度->下降;
}
删除(临时);
返回True;
}
void main(){
SkipList*list=(SkipList*)malloc(sizeof(SkipList));
SL_init(列表);
int-arr[6]={1,2,3,4,5,5};
int i;
对于(i=0;i<6;i++){
插入(列表,arr[i]);
}
/*
如果(!SL_Delete(列表,7))
printf(“不能删除”);
*/
打印(列表);
//SL_删除(列表,4);
//打印(列表);
//if(SL_查找(列表,7))
//printf(“\n查找”);
}

有人吗?请帮助:)在SL_find()中(;p->next->key next)可能应该是(;p->键next)的
不是答案,但请注意,以下划线和大写字母开头的名称保留给实现使用,这意味着您不应该在自己的代码中使用这些名称。我建议将
#ifndef SKIPLIST_H__包含在内
等作为一个可行的明确名称进行测试和定义。或者使用一些UUID作为唯一性。谢谢,但我认为这不是问题。我认为问题在于init函数或insert函数。