实现双向链表的各个功能
#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);
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(NULL == node) 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(NULL == node) 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;
}
return -1;
}
bool del_index_list(DoubleList* list,size_t index)
{
Node* node = _index_list(list,index);
if(NULL == node) return false;
_del_list(node);
list->size--;
return true;
}
bool del_value_list(DoubleList* list,TYPE data)
{
Node* node = _value_list(list,data);
if(NULL == node) return false;
_del_list(node);
list->size--;
return true;
}
void sort_list(DoubleList* list)
{
for(Node* n1=list->head->next; n1->next!=list->head; n1=n1->next)
{
for(Node* n2=n1->next; n2!=list->head; n2=n2->next)
{
if(n1->data > n2->data)
{
TYPE t = n1->data;
n1->data = n2->data;
n2->data = t;
}
}
}
}
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);
}
insert_list(list,1,9);
show_list(list);
modify_value_list(list,9,-1);
show_list(list);
modify_index_list(list,1,11);
show_list(list);
TYPE val;
access_list(list,10,&val);
printf("%d\n",val);
sort_list(list);
show_list(list);
printf("----------\n");
clean_list(list);
for(int i=0; i<10; i++)
{
add_tail_list(list,i);
}
show_list(list);
destory_list(list);
}