#include "stdlib.h"
#include <sys/types.h>
//节点
typedef struct ListNode
{
int data; //数据
struct ListNode* prev; //前指针
struct ListNode* next; //后指针
}LIST_NODE;
//链表
typedef struct list
{
LIST_NODE* head; //头指针
LIST_NODE* tail; //尾指针
LIST_NODE* frwd; //正向迭代指针
LIST_NODE* bkwd; //反向迭代指针
}LIST;
//创建节点
static LIST_NODE* create_node(int data,LIST_NODE* prev,LIST_NODE* next)
{
LIST_NODE* node =(LIST_NODE*)malloc(sizeof(LIST_NODE));
node->data=data;
node->prev=prev;
node->next=next;
return node;
}
//销毁节点
static LIST_NODE* destroy_node(LIST_NODE* node,LIST_NODE** prev)
{
LIST_NODE* next = node->next;
if (prev)
{
*prev=node->prev;
}
free(node);
return next;
}
//初始化
void list_init(LIST* list)
{
list->head=NULL;
list->tail=NULL;
}
//释放所有节点
void list_deinit(LIST* list)
{
while(list->head)
list->head = destroy_node(list->head,NULL);
list->tail = NULL;
}
//判断是否为空
int list_empty(LIST* list)
{
return !list->head && !list->tail;
}
//后插
void list_append(LIST* list,int data)
{
list->tail=create_node(data,list->tail,NULL);
if (list->tail->prev)
{
list->tail->prev->next=list->tail;
}
else
list->head=list->tail;
}
//前插
void list_apphead(LIST* list,int data)
{
list->head=create_node(data,NULL,list->head);
if (list->head->next)
{
list->head->next->prev=list->head;
}
else
list->tail=list->head;
}
//随机插
void list_insert(LIST* list,size_t pos,int data)
{
LIST_NODE* find =NULL;
for (find=list->head;find;find=find->next)
{
if (!pos--)
{
LIST_NODE* node =create_node(data,find->prev,find);
if (node->prev)
{
node->prev->next=node;
}
else
list->head=node;
node->next->prev=node;
break;
}
}
}
//随机访问
int* list_at(LIST*list,size_t pos)
{
LIST_NODE*find =NULL;
for (find=list->head;find;find=find->next)
{
if (!pos--)
{
break;
}
}
return &find->data;
}
//删除
void list_erase(LIST*list,size_t pos)
{
LIST_NODE* find=NULL;
for (find=list->head;find;find->next)
{
if (!pos--)
{
LIST_NODE* prev=NULL;
LIST_NODE* next=destroy_node(find,&prev);
if (prev)
{
prev->next=next;
}
else
list->head=next;
if (next)
{
next->prev=prev;
}
else
list->tail=prev;
break;
}
}
}
//删除匹配到的数据
void list_remove(LIST* list,int data)
{
LIST_NODE* find=NULL,*next =NULL;
for(find=list->head;find;find=next)
{
next=find->next;
if (find->data==data)
{
LIST_NODE* prev=NULL;
LIST_NODE* next=destroy_node(find,&prev);
if (prev)
{
prev->next=next;
}
else
list->head=next;
if (next)
{
next->prev=prev;
}
else
list->tail=prev;
}
}
}
//数量
size_t list_size(LIST*list)
{
size_t size=0;
LIST_NODE* node;
for (node=list->head;node;node=node->next)
{
++size;
}
return size;
}
//开始正向迭代
void list_begin(LIST* list)
{
list->frwd=list->head;
}
//继续正向迭代
int* list_next(LIST* list)
{
int* pdata = &list->frwd->data;
list->frwd=list->frwd->next;
return pdata;
}
//获取正向迭代
int* list_current(LIST* list)
{
return &list->frwd->data;
}
//终止正向迭代
void list_end(LIST* list)
{
list->frwd=list->tail->next;
}
//判断正向迭代是否终止
int list_done(LIST* list)
{
return list->frwd == list->tail->next;
}
//开始反向迭代
void list_rbegin(LIST* list)
{
list->bkwd=list->tail;
}
//继续反向迭代
int* list_rnext(LIST* list)
{
int* pdata = &list->bkwd->data;
list->bkwd=list->bkwd->prev;
return pdata;
}
//获取反向迭代
int* list_rcurrent(LIST* list)
{
return &list->bkwd->data;
}
//终止反向迭代
void list_rend(LIST* list)
{
list->bkwd=list->head->prev;
}
//判断反向迭代是否终止
int list_rdone(LIST* list)
{
return list->bkwd == list->head->prev;
}
自我学习之双向链表
最新推荐文章于 2023-03-15 19:16:06 发布