#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;
return 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 _del_list(Node* node)
{
node->prev->next=node->next;
node->next->prev=node->prev;
free(node);
}
//访问指定位置
Node* _index_list(DoubleList* list,size_t index)
{
if(index>=list->size) return NULL;
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;
}
}
//根据值访问节点
Node* _value_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;
}
//头添加
void add_head_list(DoubleList* list,TYPE data)
{
_add_list(list->head,list->head->next,data);
list->size++;
}
//尾添加
void add_tail_list(DoubleList* list,TYPE data)
{
_add_list(list->head->prev,list->head,data);
list->size++;
}
//插入
bool insert_list(DoubleList* list,size_t index,TYPE data)
{
Node* node=_index_list(list,index);
if(NULL==node) return false;
_add_list(node->prev,node,data);
list->size++;
return true;
}
//按位置修改
bool modify_index_list(DoubleList* list,size_t index,TYPE data)
{
Node* node=_index_list(list,index);
if(node==NULL) return false;
node->data=data;
return true;
}
//按值修改(全部)
int modify_value_list(DoubleList* list,TYPE old,TYPE data)
{
int cnt=0;
for(Node* n=list->head->next;n!=list->head;n=n->next)
{
if(n->data==old)
{
n->data=data;
cnt++;
}
}
return cnt;
}
//访问
bool access_list(DoubleList* list,size_t index,TYPE* val)
{
Node* node=_index_list(list,index);
if(node==NULL) return false;
*val=node->data;
return true;
}
//查询
int query_list(DoubleList* list,TYPE data)
{
Node* n=list->head->next;
for(int i=0;i<list->size;i++)
{
if(n->data==data) return i;
n=n->next;
}
}
//按位置删除
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;
}
//按值删除
bool del_value_list(DoubleList* list,TYPE data)
{
for(Node *n=list->head->next;n!=list->head;n=n->next)
{
if(n->data==data)
{
_del_list(n);
return true;
}
}
return false;
}
//排序
void sort_list(DoubleList* list)
{
for(Node* i=list->head->next;i->next!=list->head;i=i->next)
{
for(Node* j=i->next;j!=list->head;j=j->next)
{
if(i->data>j->data)
{
TYPE temp=i->data;
i->data=j->data;
j->data=temp;
}
}
}
}
//遍历
void show_list(DoubleList* list)
{
for(Node *n=list->head->next;n!=list->head;n=n->next)
{
printf("%d ",n->data);
}
printf("\n");
}
//清空
void clean_list(DoubleList* list)
{
Node* n=list->head->next;
while(n!=list->head)
{
Node* temp=n;
n=n->next;
free(temp);
}
list->head->prev=list->head;
list->head->next=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,99);
show_list(list);
modify_value_list(list,9,-1);
show_list(list);
sort_list(list);
show_list(list);
int find;
access_list(list,3,&find);
printf("%d\n",find);
int find2=-1;
find2=query_list(list,5);
printf("%d\n",find2);
}