- 论坛徽章:
- 0
|
int accessdata(void) { /* return a nonnegative key if successful */
int i;
list_t **newptrs;
if (headptr == NULL) { /* can't access a completely empty list */
errno = EINVAL;
return -1;
}
if (travptrs_size == 0) { /* first traversal */
travptrs = (list_t **)calloc(TRAV_INIT_SIZE, sizeof(list_t *));
if (travptrs == NULL) /* couldn't allocate space for traversal keys */
return -1;
travptrs[0] = headptr;
travptrs_size = TRAV_INIT_SIZE;
return 0;
}
for (i = 0; i < travptrs_size; i++) { /* look for an empty slot for key */
if (travptrs[i] == NULL) {
travptrs[i] = headptr;
return i;
}
}
newptrs = realloc(travptrs, 2*travptrs_size*sizeof(list_t *));
if (newptrs == NULL) /* couldn't expand the array of traversal keys */
return -1;
travptrs = newptrs;
travptrs[travptrs_size] = headptr;
travptrs_size *= 2;
return travptrs_size/2;
}
/*****************************************************************/
问题:最后为什么乘2 又除2啊 |
|