您的位置:首页 > 编程语言 > C语言/C++

静态双向链表的多数组实现

2017-08-27 22:50 495 查看
使用静态多数组实现链表,结构体及相关函数声明如下:

typedef struct ITEM {
int key;
void * statellite;
} item_t;
typedef struct STATIC_DOUBLE_ARRAY_DOUBLE_LINKED_LIST
{
int size;
int count;
int head;
int free;
item_t * key;
int * next;
int * prev;
} SDADLL;
void SDADLL_traverse(SDADLL * L);
SDADLL* SDADLL_init(int size);
item_t* SDADLL_get_object(SDADLL * L, int location);
int SDADLL_get_next(SDADLL * L, int location);
int SDADLL_get_prev(SDADLL * L, int location);
int SDADLL_free(SDADLL * L, int location);
int SDADLL_allocate(SDADLL * L);
int SDADLL_insert(SDADLL * L, item_t item);
int SDADLL_search(SDADLL * L, item_t item);
int SDADLL_delete(SDADLL * L, item_t item);


各函数实现如下:

//functions for static double array double linked list
SDADLL* SDADLL_init(int size) {
SDADLL * L = (SDADLL*)malloc(sizeof(SDADLL));
if (L == NULL) {
fprintf(stderr, "Static double array double linked list init fail.\n");
return NULL;
}
L->size = size;
L->count = 0;
L->head = 0;
L->free = size;
L->key = (item_t*)calloc(size, sizeof(item_t));
L->next = (int*)calloc(size, sizeof(int));
L->prev = (int*)calloc(size, sizeof(int));
if (!L->key || !L->next || !L->prev) {
fprintf(stderr, "Static double array double linked list init fail.\n");
return NULL;
}
for (int i = size - 1; i >= 0; i--) {
L->next[i] = i;
}
return L;
}
item_t* SDADLL_get_object(SDADLL * L, int loc) {
return L->key + loc - 1;
}
int SDADLL_get_next(SDADLL * L, int loc) {
return L->next[loc - 1];
}
int SDADLL_get_prev(SDADLL * L, int loc) {
return L->prev[loc - 1];
}
int SDADLL_free(SDADLL * L, int location) {
if (location <= 0 || location > L->size) {
fprintf(stderr, "Location out of range.\n");
return 0;
}
L->next[location - 1] = L->free;
L->free = location;
return 1;
}
int SDADLL_allocate(SDADLL * L) {
if (L->free == 0) {
fprintf(stderr, "Out of range.\n");
return 0;
}
int x = L->free;
L->free = L->next[L->free - 1];
return x;
}
int SDADLL_insert(SDADLL * L, item_t item) {
int obj = SDADLL_allocate(L);
if (obj == 0) {
fprintf(stderr, "Out of range.\n");
return 0;
}
L->count++;
L->key[obj - 1] = item;
L->next[obj - 1] = L->head;
L->prev[obj - 1] = 0;
if (L->head != 0)
L->prev[L->head - 1] = obj;
L->head = obj;
return obj;
}
int SDADLL_search(SDADLL * 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 loc = L->head;
item_t * it = NULL;
while (loc != 0) {
it = SDADLL_get_object(L, loc);
if (it->key == item.key)
return loc;
loc = SDADLL_get_next(L, loc);
}
fprintf(stderr, "Item cannot be found.\n");
return 0;
}
int SDADLL_delete(SDADLL * L, item_t item) {
int location = SDADLL_search(L, item);
if (location == 0) {
fprintf(stderr, "Delete fail.\n");
return 0;
}
if (L->prev[location - 1] != 0)
L->next[SDADLL_get_prev(L, location) - 1] = SDADLL_get_next(L, location);
if (L->next[location - 1] != 0)
L->prev[SDADLL_get_next(L, location) - 1] = SDADLL_get_prev(L, location);
L->count--;
if (location == L->head)
L->head = SDADLL_get_next(L, location);
return SDADLL_free(L, location);
}
void SDADLL_traverse(SDADLL * 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.\n", L->count, L->size);
while (location != 0) {

b997
printf("%3d item prev is %3d, location is %3d, next is %3d.\n", \
SDADLL_get_object(L, location)->key, SDADLL_get_prev(L, location), location, \
SDADLL_get_next(L, location));
location = SDADLL_get_next(L, location);
}
int free = L->free;
while (free != 0) {
printf("Free space is %3d, next is: %3d\n", free, L->next[free - 1]);
free = L->next[free - 1];
}
}
//--------------------------------------------------------------------------


该实现中,对于location定义为从1到size的任意整数。

可用如下函数作为测试:

void test_for_SDADLL() {
SDADLL * L = SDADLL_init(SIZE);
for (int i = 0; i < 10; i++) {
item_t item = {i + 10, NULL};
SDADLL_insert(L,item);
}
for (int i = 10; i >= -1; i--) {
item_t item = {i + 10, NULL};
SDADLL_delete(L, item);
}
SDADLL_traverse(L);
printf("-------------------------------------------------------------------\n");
for (int i = 0; i <= 10; i++) {
item_t item = {i + 10, NULL};
SDADLL_insert(L, item);
}
for (int i = 0; i <= 5; i++) {
item_t item = {i + 10, NULL};
SDADLL_delete(L, item);
}
for (int i = 0; i <= 10; i++) {
item_t item = {i - 10, NULL};
SDADLL_insert(L, item);
}
for (int i = -10; i <= 17; i++) {
item_t item = {i, NULL};
SDADLL_delete(L, item);
}
SDADLL_traverse(L);
}


关于静态双向链表的多数组方式实现的内容,可参考算法导论第三版10.3章。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息