- Node.h
#ifndef NODE_H
#define NODE_H
#include
#include
typedef struct NODE
{
int value;
struct NODE *prior, *next;
}Node, *DNode;
#endif
- List.h
#ifndef LIST_H
#define LIST_H
#include "Node.h"
void InitDouLinkList(DNode *pHead);
void InsertByHead(DNode *pHead, int value);
void InsertByTail(DNode *pHead, int value);
int GetLength(DNode pHead);
void ShowDouLinkList(DNode pHead);
void ReverseShowLinkList(DNode pHead);
void DeleteByHead(DNode *pHead);
void DeleteByTail(DNode *pHead);
DNode FindByValue(DNode pHead, int value);
DNode FindByIndex(DNode pHead, int index);
void InsertByIndex(DNode *pHead, int index, int value);
void DeleteByIndex(DNode* pHead, int index);
void UpdateByIndex(DNode pHead, int index, int value);
void UpdateByValue(DNode pHead, int oldval, int newval);
void DeleteByValue(DNode *pHead, int value);
void Clear(DNode* pHead);
#endif
- List.c
void InitDouLinkList(DNode *pHead)
{
*pHead = (DNode)malloc(sizeof(Node));
(*pHead)->prior = *pHead;
(*pHead)->next = *pHead;
}
void InsertByHead(DNode *pHead, int value)
{
DNode pNew = (DNode)malloc(sizeof(Node));
pNew->value = value;
if ((*pHead)->next == *pHead || (*pHead)->prior == *pHead)
{
(*pHead)->next = pNew;
(*pHead)->prior = pNew;
pNew->next = *pHead;
pNew->prior = *pHead;
}
else
{
(*pHead)->next->prior = pNew;
pNew->next = (*pHead)->next;
pNew->prior = (*pHead);
(*pHead)->next = pNew;
}
}
void InsertByTail(DNode *pHead, int value)
{
if ((*pHead)->next == *pHead || (*pHead)->prior == *pHead)
{
InsertByHead(pHead, value);
}
else
{
DNode pNew = (DNode)malloc(sizeof(Node));
pNew->value = value;
(*pHead)->prior->next = pNew;
pNew->prior = (*pHead)->prior;
pNew->next = *pHead;
(*pHead)->prior = pNew;
}
}
int GetLength(DNode pHead)
{
int count = 0;
DNode pos = pHead;
while (pos->next != pHead)
{
pos = pos->next;
count++;
}
return count;
}
void ShowDouLinkList(DNode pHead)
{
DNode pos = pHead;
while (pos->next != pHead)
{
pos = pos->next;
//pHead里面存的是第一个结点的地址,pHead->next->value是第一个数
printf("%d\t", pos->value);
}
}
void ReverseShowLinkList(DNode pHead)
{
DNode pos = pHead;
while (pos->prior != pHead)
{
pos = pos->prior;
printf("%d\t", pos->value);
}
}
void DeleteByHead(DNode *pHead)
{
if ((*pHead)->next != *pHead || (*pHead)->prior != *pHead)
{
DNode temp = (*pHead)->next;
(*pHead)->next = temp->next;
temp->next->prior = (*pHead);
free(temp);
temp = NULL;
}
}
void DeleteByTail(DNode *pHead)
{
if ((*pHead)->next != *pHead || (*pHead)->prior != *pHead)
{
DNode temp = (*pHead)->prior;
temp->prior->next = *pHead;
*pHead = temp->next;
free(temp);
temp = NULL;
}
}
DNode FindByValue(DNode pHead, int value)
{
DNode pos = pHead;
while (pos->value != value&&pos->next != pHead)
{
pos = pos->next;
}
return pos;
}
DNode FindByIndex(DNode pHead, int index)
{
DNode pos = pHead;
if (index >0 && indexfor (int i = 0; i <= index; i++)
{
pos = pos->next;
}
}
return pos;
}
void InsertByIndex(DNode *pHead, int index, int value)
{
if (0 == index)
{
InsertByHead(pHead, value);
}
else if (index > 0 && index <= GetLength(*pHead))
{
DNode pos = *pHead;
for (int i = 0; i < index && pos->next != *pHead; i++)
{
pos = pos->next;
}
DNode pNew = (DNode)malloc(sizeof(Node));
pNew->value = value;
pNew->next = pos->next;
pos->prior = pNew;
pNew->prior = pos;
pos->next = pNew;
}
}
void DeleteByIndex(DNode* pHead, int index)
{
DNode pos = FindByIndex(*pHead, index);
if (pos != NULL)
{
if (pos->next != *pHead)
{
pos->prior->next = pos->next;
pos->next->prior = pos->prior;
free(pos);
pos = NULL;
}
else
{
pos->prior->next = pos->next;
pos->next->prior = pos->prior;
free(pos);
pos = NULL;
}
}
}
void UpdateByIndex(DNode pHead, int index, int value)
{
DNode pos = FindByIndex(pHead, index);
if (pos != NULL)
{
pos->value = value;
}
}
void UpdateByValue(DNode pHead, int oldval, int newval)
{
DNode pos = FindByValue(pHead, oldval);
while (pos != NULL&&pos->value == oldval)
{
pos->value = newval;
pos = FindByValue(pHead, oldval);
}
}
void DeleteByValue(DNode *pHead, int value)
{
DNode pos = FindByValue(*pHead, value);
while (pos != NULL&&pos->next != *pHead)
{
pos->prior->next = pos->next;
pos->next->prior = pos->prior;
free(pos);
pos = NULL;
pos = FindByValue(*pHead, value);
}
if (pos->next == *pHead && pos != NULL)
{
DeleteByTail(pHead);
}
}
void Clear(DNode* pHead)
{
while ((*pHead)->next != *pHead || (*pHead)->prior != *pHead)
{
DeleteByTail(pHead);
}
}
- main.c
#include "Node.h"
#include "List.h"
int main(void)
{
DNode pHead = NULL;
InitDouLinkList(&pHead);
InsertByTail(&pHead, 5);
InsertByTail(&pHead, 7);
InsertByTail(&pHead, 8);
InsertByTail(&pHead, 7);
InsertByTail(&pHead, 8);
Clear(&pHead);
ShowDouLinkList(pHead);
return 0;
}