#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define TYPE int
// 设计双向链表节点
typedef struct Node
{
struct Node* prev;
TYPE data;
struct Node* next;
}Node;
// 创建节点
Node* create_node(TYPE data)
{
Node* node = malloc(sizeof(Node));
node->prev = node;
node->data = data;
node->next = node;
}
// 设计双向链表结构
typedef struct DoubleList
{
Node* head; // 带头节点
size_t size;
}DoubleList;
// 创建链表
DoubleList* create_list(void)
{
DoubleList* list = malloc(sizeof(DoubleList));
list->head = create_node(0);
list->size = 0;
return list;
}
// 在前趋和后继之间添加一个节点
// 下划线开头表示该函数是依赖函数
// 头添加的依赖
void _add_list(Node* p,Node* n,TYPE data)
{
Node* node = create_node(data);
node->prev = p;
node->next = n;
p->next = node;
n->prev = node;
}
// 头添加
void add_head_list(DoubleList* list,TYPE data)
{
_add_list(list->head,list->head->next);
list->size++;
}
// 尾添加
void add_tail_list(Double* list,TYPE data)
{
_add_list(list->head->prev,list->head);
list->size++;
}
// 访问指定位置的节点,插入、按位置修改或删除的依赖
Node* _index_list(DoubleList* list,size_t index)
{
if(list->size <= index) return false;
if(index < list->size/2)
{
// 从前往后遍历
Node* n = list->head->next;
while(index--) n = n->next;
return n;
}
else
{
// 从后往前遍历
Node* n = list->head->prev;
while(++index < list->size) n = n->prev;
return n;
}
}
// 插入
bool insert_list(DoubleList* list,size_t index,TYPE data)
{
Node* node = _index_list(list,index);
if(NULL == node) return false;
_add_list(list,data);
lsit->size++;
return true;
}
// 按位置修改
bool modify_index_list(DoubleList* list,size_t index,TYPE data)
{
Node* node = _index_list(list,index);
if(node == NULL) return flase;
node->data = data;
return true;
}
// 按值修改、更新
int modify_value_list(DoubleList* list,TYPE old,TYPE new)
{
int cnt = 0; // 记录修改次数
for(Node* n = list->head->next;n!=list->head;n=n->next)
{
if(n->data == old)
{
n->data = new;
cnt++;
}
}
return cnt;
}
// 访问
bool access_list(DoubleList* list,size_t index,TYPE* data)
{
Node* node = _index_list(list,index);
if(node == NULL) return false;
*data = node->data;
return true;
}
// 查询
int query_list(DoubleList* list,TYPE data)
{
Node* node = list->head->next;
for(int i=0;i < list->size;i++)
{
if(node->data == data) return i;
n = n->next;
}
return -1;
}
// 删除当前节点
void _del_list(Node* n)
{
n->prev->next = n->next;
n->next->prev = n->prev;
free(n);
}
// 按位置删除
bool del_index_list(DoubleList* list,size_t index)
{
Node* node = _index_list(list,index);
if(node == NULL) return false;
_del_list(node);
return true;
}
// 按值删除的依赖
Node* _val_list(DoubleList* list,TYPE data)
{
for(Node* n = list->head->next;n!=list->head;n=n->next)
{
if(n->data == data) return n;
}
return NULL;
}
// 按值删除
bool del_val_list(DoubleList* list,TYPE data)
{
Node* node = _val_list(list,data);
if(node == NULL) return false;
_del_list(node);
return false;
}
// 遍历
void show(DouleList* list)
{
for(Node* node = list->head->next;n!=list->head;n=n->next)
{
printf("%d ",n->data);
}
printf("\n");
}
// 清空
void clean_list(DoubleList* list)
{
Node* node = list->head->next;
while(n!=list->head)
{
Node* temp = node;
node = node->next;
free(temp);
}
list->head->next = list->head;
list->head->prev = list->head;
list->size = 0;
}
// 销毁
void destory_list(DoubleList* list)
{
clean_list(list);
free(list->head);
free(list);
}
int main(int argc,const char* argv[])
{
DoubleList* list = create_list();
for(int i=0; i<10; i++)
{
add_tail_list(list,i);
}
show_list(list);
insert_list(list,0,100);
insert_list(list,3,100);
insert_list(list,6,100);
show_list(list);
modify_value_list(list,100,999);
show_list(list);
TYPE num = 0;
access_list(list,12,&num);
printf("num=%d\n",num);
printf("index=%d\n",query_list(list,1));
del_index_list(list,12);
del_value_list(list,8);
show_list(list);
clean_list(list);
// destory_list(list);
printf("--------\n");
for(int i=0; i<10; i++)
{
add_tail_list(list,i);
}
show_list(list);
}
09-14