双向链表的表示和实现
表示
typedef struct node_t { data_t data; struct node_t *prior, *next; } linknode_t, *linklist_t;
实现
//创建 linklist_t CreateEmptyLinklist() { linklist_t list; list = (linklist_t)malloc(sizeof(linknode_t)); if (NULL != list) { list->prior = NULL; list->next = NULL; return list; } else { return NULL; } } //清空 int ClearLinklist(linklist_t list) { linknode_t *node; if (NULL != list) { while (NULL != list->next) { node = list->next; list->next = node->next; free(node); } return 0; } else { return -1; } } //销毁 int DestroyLinklist(linklist_t list) { if (NULL != list) { ClearLinklist(list); free(list); return 0; } else { return -1; } } //是否为空 int EmptyLinklist(linklist_t list) { if (NULL != list) { if (NULL != list->next) return 1; else return 0; } else { return -1; } } //当前长度 int LengthLinklist(linklist_t list) { int i = 0; linknode_t *node; if (NULL != list) { node = list->next; while (NULL != node) { node = node->next; i++; } return i; } else { return -1; } } //查 int GetLinklist(linklist_t list, int at, data_t *x) { int i = 0; linknode_t *node; if (NULL != list) { if (at < 0) return -1; node = list->next; while (NULL != node) { if (at == i) { if (x != NULL) *x = node->data; return 0; } node = node->next; i++; } } else { return -1; } } //改 int SetLinklist(linklist_t list, int at, data_t x) { int i = 0; linknode_t *node; int found = 0; if (NULL != list) { if (at < 0) return -1; node = list->next; while (NULL != node) { if (at == i) { node->data = x; found = 1; break; } node = node->next; i++; } } else { return -1; } if (found == 1) return 0; else return -1; } //增 int InsertLinklist(linklist_t list, int at, data_t x) { int i = 0; linknode_t *node_at, *node_prev, *node_new; int found = 0; if (at < 0) return -1; if (NULL != list) { node_new = (linknode_t *)malloc(sizeof(linknode_t)); if (NULL == node_new) { return -1; } node_new->data = x; node_new->next = NULL; node_new->prior = NULL; node_prev = list; node_at = list->next; while (NULL != node_at) { if (at ==i) { found = 1; break; } node_prev = node_at; node_at = node_at->next; i++; } } else { return -1; } if (found) { node_new->next = node_prev->next; node_new->prior = node_prev; node_prev->next = node_new; node_at->prior = node_new; } else { node_prev->next = node_new; node_new->prior = node_prev; } return 0; } //删 int DeleteLinklist(linklist_t list, int at) { linknode_t *node_prev, *node_at; int pos = 0; int found = 0; if (NULL != list) { node_prev = list; node_at = list->next; if (at < 0) return -1; while (NULL != node_at->next) { if (pos == at) { found = 1; break; } node_prev = node_at; node_at = node_at->next; pos++; } } else { return -1; } if (found == 1) { node_prev->next = node_at->next; linknode_t *node = node_at->next; if (node != NULL) node->prior = node_prev; free(node_at); return 0; } else { return -1; } }
测试代码
void iterate_list(linklist_t list) { linknode_t *node; if (!list) return; printf("list = {"); /* start from the first element */ node = list->next; while (NULL != node) { printf("%d->", node->data); /* move to the next */ node = node->next; } if (LengthLinklist(list) > 0) printf("} "); else printf("} "); } int main(int argc, char *argv[]) { int i; data_t a[10] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; data_t x; linklist_t list; list = CreateEmptyLinklist(); if (NULL == list) return -1; printf("insert method 1: insert each elment "); for (i = 0; i < 10; i++) { if (InsertLinklist(list, i, a[i]) < 0) break; } iterate_list(list); GetLinklist(list, 4, &x); printf("list[4] = %d ", x); printf("updated list[4] to 100 "); SetLinklist(list, 4, 100); GetLinklist(list, 4, &x); printf("now list[4] = %d ", x); iterate_list(list); printf("removed list[4] "); DeleteLinklist(list, 4); GetLinklist(list, 4, &x); printf("now list[4] = %d ", x); printf("and total number of list is %d ", LengthLinklist(list)); iterate_list(list); printf("insert "1" at the %dth position of the list ", 0); InsertLinklist(list, 0, 1); iterate_list(list); ClearLinklist(list); printf("after clear, total number of list is %d and ", LengthLinklist(list)); iterate_list(list); DestroyLinklist(list); return 0; }
结果
insert method 1: insert each elment list = {2->4->6->8->10->12->14->16->18->20} list[4] = 10 updated list[4] to 100 now list[4] = 100 list = {2->4->6->8->100->12->14->16->18->20} removed list[4] now list[4] = 12 and total number of list is 9 list = {2->4->6->8->12->14->16->18->20} insert "1" at the 0th position of the list list = {1->2->4->6->8->12->14->16->18->20} after clear, total number of list is 0 and list = {}