Wednesday, 11 September 2013

Skip list source code from book , A structure confused me

Skip list source code from book , A structure confused me

I have googled skip list and found a book "Ontroduction to Algorithms"
http://epaperpress.com/sortsearch/index.html and get the skip list
tutorial sample from the following
http://epaperpress.com/sortsearch/skl.html
There is a skl.c which is running well , but as I study the code I have
found something confused me , showes as the follwoing :
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 */
and the function confused me is the following :
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;
}
Seems MAXLEVEL = 15 in this test , so in initList() , it would do
list.hdr->forward[0] = NIL; to list.hdr->forward[15] = NIL; and look at
nodeType structure , it has the forward var struct nodeTag *forward[1]; ,
not struct nodeTag *forward[MAXLEVEL];
I think the correct structure should be :
typedef struct nodeTag {
keyType key; /* key used for searching */
recType rec; /* user data */
struct nodeTag *forward[MAXLEVEL]; /* skip list forward pointer */
} nodeType;
not
typedef struct nodeTag {
keyType key; /* key used for searching */
recType rec; /* user data */
struct nodeTag *forward[1]; /* skip list forward pointer */
} nodeType;
Or I missed something ?

No comments:

Post a Comment