我用谷歌搜索了跳过列表并找到了一本书“算法简介” http://epaperpress.com/sortsearch/index.html 并从以下 http://epaperpress.com/sortsearch/skl.html获取了跳过列表教程示例
有一个运行良好的 skl.c,但是当我研究代码时,我发现一些让我感到困惑的东西,如下所示:
typedef int keyType; /* type of key */
/* user data stored in tree */
typedef struct {
int stuff; /* optional related data */
} recType;
/* levels range from (0 .. MAXLEVEL) */
#define MAXLEVEL 15
typedef struct nodeTag {
keyType key; /* key used for searching */
recType rec; /* user data */
struct nodeTag *forward[1]; /* skip list forward pointer */
} nodeType;
/* implementation independent declarations */
typedef struct {
nodeType *hdr; /* list Header */
int listLevel; /* current level of list */
} SkipList;
SkipList list; /* skip list information */
让我感到困惑的功能如下:
void initList() {
int i;
/**************************
* initialize skip list *
**************************/
if ((list.hdr = malloc(
sizeof(nodeType) + MAXLEVEL*sizeof(nodeType *))) == 0) {
printf ("insufficient memory (initList)\n");
exit(1);
}
for (i = 0; i <= MAXLEVEL; i++)
list.hdr->forward[i] = NIL;
list.listLevel = 0;
}
在这个测试中似乎 MAXLEVEL = 15 ,所以在 initList() ,它会做 list.hdr->forward[0] = NIL; 到 list.hdr->forward[15] = NIL; 看看 nodeType 结构,它有 forward var struct nodeTag *forward[1]; , 不是结构 nodeTag *forward[MAXLEVEL];
我认为正确的结构应该是:
typedef struct nodeTag {
keyType key; /* key used for searching */
recType rec; /* user data */
struct nodeTag *forward[MAXLEVEL]; /* skip list forward pointer */
} nodeType;
不是
typedef struct nodeTag {
keyType key; /* key used for searching */
recType rec; /* user data */
struct nodeTag *forward[1]; /* skip list forward pointer */
} nodeType;
或者我错过了什么?