#pragma once
typedef char DLinkType;
typedef struct DLinkNode {
DLinkType data;
struct DLinkNode* next;
struct DLinkNode* prev;
} DLinkNode;
void DLinkListInit(DLinkNode** head);
DLinkNode* DLinkListPushBack(DLinkNode* head, DLinkType value);
void DLinListPopBack(DLinkNode* head);
void DLinkListPushFront(DLinkNode* head, DLinkType value);
void DLinkListPopFront(DLinkNode* head);
DLinkNode* DLinkListFind(DLinkNode* head, DLinkType to_find);
/**
* * @brief 往指定位置之前插入一个元素
* */
void DLinkListInsert(DLinkNode* pos, DLinkType value);
/**
* * @brief 往指定位置之后插入一个元素
* */
void DLinkListInsertAfter(DLinkNode* pos, DLinkType value);
void DLinkListErase(DLinkNode* head,DLinkType value);
void DLinkListRemove(DLinkNode* head,DLinkNode* pos);
void DLinkListRemoveAll(DLinkNode* head);
size_t DLinkListSize(DLinkNode* head);
int DLinkListEmpty(DLinkNode* head);
#include<stdio.h>
#include <stdlib.h>
#include"doubly_linked_list.h"
DLinkNode* CreatNode(DLinkType value)
{
DLinkNode* node= (DLinkNode*)malloc(sizeof(DLinkNode));
node->data=value;
}
void DLinkListInit(DLinkNode** head)
{
*head= (DLinkNode*)malloc(sizeof(DLinkNode));
(*head)->data=0;
(*head)->next=*head;
(*head)->prev=*head;
}
DLinkNode* DLinkListPushBack(DLinkNode* head, DLinkType value)
{
if(head==NULL)
{
return NULL;
}
DLinkNode* tmp=CreatNode(value);
DLinkNode* tail=head->prev;
head->prev=tmp;
tmp->next=head;
tmp->prev=tail;
tail->next=tmp;
return head;
}
void DLinListPopBack(DLinkNode* head)
{
if(head==NULL)
{
return;
}
if(head->prev==head)
{
return;
}
DLinkNode * to_delete=head->prev;
DLinkNode * tail=to_delete->prev;
tail->next=head;
head->prev=tail;
free(to_delete) ;
}
void DLinkListPushFront(DLinkNode* head, DLinkType value)
{
if(head==NULL)
{
return;
}
DLinkNode* tmp =CreatNode(value);
DLinkNode* first=head->next;
tmp->next=first;
first->prev=tmp;
head->next=tmp;
tmp->prev=head;
}
void DLinkListPopFront(DLinkNode* head)
{
if(head==NULL)
{
return;
}
if(head->next=head)
{
return;
}
DLinkNode* to_delete=head->next;
DLinkNode* first=to_delete->next;
head->next=first;
first->prev=head;
free(to_delete);
}
DLinkNode* DLinkListFind(DLinkNode* head, DLinkType to_find)
{
if(head==NULL)
{
return NULL;
}
if(head->next=head)
{
return NULL;
}
DLinkNode* cur=head->next;
while(cur!=head)
{
if(cur->data==to_find)
{
return cur;
}
cur=cur->next;
}
return NULL;
}
/**
* * @brief 往指定位置之前插入一个元素
* */
void DLinkListInsert(DLinkNode* pos, DLinkType value)
{
if(pos==NULL)
{
return;
}
DLinkNode* pre=pos->prev;
DLinkNode* tmp=CreatNode(value);
tmp->prev=pre;
pre->next=tmp;
tmp->next=pos;
pos->prev=tmp;
}
/**
* * @brief 往指定位置之后插入一个元素
* */
void DLinkListInsertAfter(DLinkNode* pos, DLinkType value)
{
if(pos==NULL)
{
return;
}
DLinkNode* tail=NULL;
DLinkNode* tmp=CreatNode(value);
tail=pos->next;
pos->next=tmp;
tmp->prev=pos;
tmp->next=tail;
tail->prev=tmp;
}
void DestoryNode(DLinkNode* to_delete)
{
if(to_delete==NULL)
{
return;
}
DLinkNode* pre=to_delete->prev;
DLinkNode* tal=to_delete->next;
pre->next=tal;
tal->prev=pre;
free(to_delete);
}
void DLinkListErase(DLinkNode* head,DLinkType value)
{
if(head==NULL)
{
return;
}
if(head->next=head)
{
return;
}
DLinkNode*cur=head->next;
while(cur!=head)
{
if(cur->data==value)
{
DestoryNode(cur);
}
cur=cur->next;
}
}
void DLinkListRemove(DLinkNode* head,DLinkNode* pos)
{
if(head==NULL)
{
return;
}
if(head->next=head)
{
return;
}
DestoryNode(pos);
}
void DLinkListRemoveAll(DLinkNode* head)
{
if(head==NULL)
{
return;
}
if(head->next=head)
{
return;
}
DLinkNode* cur=head->next;
DLinkNode* later=cur->next;
while(cur!=head)
{
DestoryNode(cur);
cur=later;
later=later->next;
}
}
size_t DLinkListSize(DLinkNode* head)
{
size_t count=0;
DLinkNode* cur=head->next;
while(cur!=head)
{
count++;
cur=cur->next;
}
return count;
}
int DLinkListEmpty(DLinkNode* head)
{
if(head==NULL)
{
return -1;
}
if(head=head->next)
{
return 0;
}
else
{
return 1;
}
}
int main()
{
return 0;
}