题目:
我们往往希望双向链表的所有元素在存储器中保持紧凑,例如,在多数组表示中,占用前m个下标位置。(在页式虚拟存储的计算环境下,即为这种情况。)假设除指向聊表本身的指针外没有其他指针指向该链表元素,试说明如何实现过程ALLOCATE-OBJECT和FREE-OBJECT,使得该表保持紧凑。(提示:使用栈的数组实现。)
解答:
在某位大牛的算法导论中的答案中,分配链表元素位置时从数组最前向最后分配空间,若删除一个元素,且该元素在存储链表的数组中并非最后一个元素,则需将数组中处于该位置右端的所有链表元素左移。该种方法一次删除操作将消耗线性时间。
本文中采用的策略有所不同。分配链表元素位置时,也是从存储链表的数组的最前断向后分配。有如下约定:
L->free的位置之后的所有数组均为空,包含L->free位置均为可分配的位置。
每次删除除了L->free - 1位置的链表元素时,需将L->free - 1位置的元素移到被删除元素的位置,并将调整该元素前后元素的prev和next指向的位置,接着将L->free减去1,此时仍然保持了L->free和其后的所有数组元素均处于待分配状态。
一些函数和结构体的声明如下:
typedef struct TIGHT_DOUBLE_ARRAY_DOUBLE_LINKED_LIST
{
int size;
int count;
int head;
int free;
item_t * key;
int * next;
int * prev;
} TDADLL;
void TDADLL_traverse(TDADLL * L);
TDADLL* TDADLL_init(int size);
item_t* TDADLL_get_object(TDADLL * L, int location);
int TDADLL_get_next(TDADLL * L, int location);
void TDADLL_set_next(TDADLL * L, int location, int next);
int TDADLL_get_prev(TDADLL * L, int location);
void TDADLL_set_prev(TDADLL * L, int location, int prev);
void TDADLL_move(TDADLL * L, int src, int dest);
int TDADLL_free(TDADLL * L, int location);
int TDADLL_allocate(TDADLL * L);
int TDADLL_insert(TDADLL * L, item_t item);
int TDADLL_search(TDADLL * L, item_t item);
int TDADLL_delete(TDADLL * L, item_t item);
实现如下:
//functions for tight static double array double linked list
void TDADLL_traverse(TDADLL * L) {
if (L == NULL) {
fprintf(stderr, "Not initialized.\n");
return;
}
if (L->head == 0) {
fprintf(stderr, "Empty linked list.\n");
}
int location = L->head;
printf("Total elements number is %3d. Linked list size is %3d. Linked list head is %d\n", \
L->count, L->size, L->head);
while (location != 0) {
printf("%3d item prev is %3d, location is %3d, next is %3d.\n", \
TDADLL_get_object(L, location)->key, TDADLL_get_prev(L, location), location, \
TDADLL_get_next(L, location));
location = TDADLL_get_next(L, location);
}
printf("Free space start postion is %3d\n", L->free);
}
TDADLL* TDADLL_init(int size) {
TDADLL * L = (TDADLL*)malloc(sizeof(TDADLL));
if (!L) {
fprintf(stderr, "Tight static double array double linked list init fail.\n");
return NULL;
}
L->size = size;
L->count = 0;
L->head = 0;
L->free = 1;
L->key = (item_t*)calloc(size, sizeof(item_t));
L->prev = (int*)calloc(size, sizeof(int));
L->next = (int*)calloc(size, sizeof(int));
if (!L->key || !L->next || !L->prev) {
fprintf(stderr, "Tighe static double array double linked list content init fail.\n");
return NULL;
}
return L;
}
item_t* TDADLL_get_object(TDADLL * L, int location) {
return L->key + location - 1;
}
int TDADLL_get_next(TDADLL * L, int location) {
return L->next[location - 1];
}
void TDADLL_set_next(TDADLL * L, int location, int next) {
L->next[location - 1] = next;
}
int TDADLL_get_prev(TDADLL * L, int location) {
return L->prev[location - 1];
}
void TDADLL_set_prev(TDADLL * L, int location, int prev) {
L->prev[location - 1] = prev;
}
void TDADLL_move(TDADLL * L, int src, int dest) {
if (src <= 0 || src > L->size || dest <= 0 || dest > L->size) {
fprintf(stderr, "Move Out of range.\n");
return;
}
L->next[dest - 1] = L->next[src - 1];
L->prev[dest - 1] = L->prev[src - 1];
L->key[dest - 1] = L->key[src - 1];
}
int TDADLL_free(TDADLL * L, int location) {
if (location <= 0 || location > L->size) {
fprintf(stderr, "Free %d Out of range.\n", location);
return 0;
}
if (location >= L->free && L->free != 0) {
fprintf(stderr, "Free %d Out of range.\n", location);
return 0;
}
if (L->free == 0) {
TDADLL_move(L, L->size, location);
L->free = L->size;
if (L->size == L->head)
L->head = location;
} else if (L->free != location + 1) {
TDADLL_move(L, L->free - 1, location);
L->free = L->free -1;
if (L->free == L->head)
L->head = location;
} else {
L->free--;
return 1;
}
//printf("location: %d free:%d\n", location, L->free);
int next = TDADLL_get_next(L, location);
int prev = TDADLL_get_prev(L, location);
if (next != 0) {
TDADLL_set_prev(L, next, location);
}
if (prev != 0) {
TDADLL_set_next(L, prev, location);
}
return 1;
}
int TDADLL_allocate(TDADLL * L) {
if (L->free == 0) {
fprintf(stderr, "Allocatet Out of range.\n");
return 0;
}
int location = L->free;
if (L->free == L->size) {
L->free = 0;
} else {
L->free++;
}
return location;
}
int TDADLL_insert(TDADLL * L, item_t item) {
if (L == NULL) {
fprintf(stderr, "Not initialized.\n");
return 0;
}
int location = TDADLL_allocate(L);
if (location == 0) {
fprintf(stderr, "Allocate location fail.\n");
return location;
}
TDADLL_set_next(L, location, L->head);
TDADLL_set_prev(L, location, 0);
*TDADLL_get_object(L, location) = item;
if (L->head != 0) {
TDADLL_set_prev(L, L->head, location);
}
L->count++;
L->head = location;
return location;
}
int TDADLL_search(TDADLL * L, item_t item) {
if (L == NULL) {
fprintf(stderr, "Not initialized.\n");
return 0;
}
if (L->head == 0) {
fprintf(stderr, "Empty linked list.\n");
return 0;
}
int location = L->head;
while (location != 0) {
if (TDADLL_get_object(L, location)->key == item.key) {
return location;
}
location = TDADLL_get_next(L, location);
}
fprintf(stderr, "Item %d cannot be found.\n", item.key);
return 0;
}
int TDADLL_delete(TDADLL * L, item_t item) {
int location = TDADLL_search(L, item);
if (location == 0) {
fprintf(stderr, "Delete %d fail.\n", item.key);
return 0;
}
int next = TDADLL_get_next(L, location);
int prev = TDADLL_get_prev(L, location);
//printf("location:%d next:%d prev:%d\n", location, next, prev);
if (next != 0) {
TDADLL_set_prev(L, next, prev);
}
if (prev != 0) {
TDADLL_set_next(L, prev, next);
}
if (location == L->head) {
L->head = next;
}
L->count--;
//TDADLL_traverse(L);
return TDADLL_free(L, location);
}
//--------------------------------------------------------------------------
可用如下代码进行测试:
void test_for_TDADLL() {
TDADLL * L = TDADLL_init(SIZE);
item_t item = {0, NULL};
for (int i = 0; i < SIZE; ++i)
{
item.key = i;
TDADLL_insert(L, item);
}
TDADLL_traverse(L);
for (int i = 0; i < 10; ++i)
{
item.key = i + 10;
TDADLL_delete(L, item);
}
TDADLL_traverse(L);
for (int i = 0; i < SIZE; ++i)
{
item.key = i;
TDADLL_delete(L, item);
}
TDADLL_traverse(L);
printf("-------------------------------------------------------------------\n");
for (int i = 0; i < SIZE; ++i)
{
item.key = i;
TDADLL_insert(L, item);
}
TDADLL_traverse(L);
for (int i = 0; i < 10; ++i)
{
item.key = i + 10;
TDADLL_delete(L, item);
}
TDADLL_traverse(L);
for (int i = SIZE; i >= -1; --i)
{
item.key = i;
TDADLL_delete(L, item);
}
TDADLL_traverse(L);
}