0.定义双链表数据结构
typedef struct DNode
{
Elemtype data;
struct DNode* prior, * next;
}DNode, * DLinkList;
1.初始化双链表
Status InitDLinkList(DLinkList& L)
{
L = (DNode*)malloc(sizeof(DNode));
if (L == NULL)
return false;
L->prior = NULL;
L->next = NULL;
return true;
}
2.判断双链表是否为空(带头结头)
bool Empty(DLinkList L)
{
if (L->next == NULL)
{
return true;
}
else
{
return false;
}
}
3.在p结点之后插入s结点
Status InsertNextDNode(DNode* p, DNode* s)
{
if (p == NULL || s == NULL)
{
return false;
}
s->next = p->next;
if (p->next != NULL)
p->next->prior = s;
s->prior = p;
p->next = s;
return true;
}
4.删除p结点的后继结点
Status DeleteNextDNode(DNode* p)
{
if (p == NULL)
return false;
DNode* q = p->next;
if (q == NULL)
return false;
p->next = q->next;
if (q->next != NULL)
q->next->prior = p;
free(q);
return true;
}
5.销毁
void DestoryList(DLinkList& L)
{
while (L->next != NULL)
{
DeleteNextDNode(L);
}
free(L);
L = NULL;
}
完整代码
#include<stdio.h>
#include<stdlib.h>
typedef int Elemtype;
typedef int Status;
typedef struct DNode
{
Elemtype data;
struct DNode* prior, * next;
}DNode, * DLinkList;
Status InitDLinkList(DLinkList& L)
{
L = (DNode*)malloc(sizeof(DNode));
if (L == NULL)
return false;
L->prior = NULL;
L->next = NULL;
return true;
}
bool Empty(DLinkList L)
{
if (L->next == NULL)
{
return true;
}
else
{
return false;
}
}
Status InsertNextDNode(DNode* p, DNode* s)
{
if (p == NULL || s == NULL)
{
return false;
}
s->next = p->next;
if (p->next != NULL)
p->next->prior = s;
s->prior = p;
p->next = s;
return true;
}
Status DeleteNextDNode(DNode* p)
{
if (p == NULL)
return false;
DNode* q = p->next;
if (q == NULL)
return false;
p->next = q->next;
if (q->next != NULL)
q->next->prior = p;
free(q);
return true;
}
void DestoryList(DLinkList& L)
{
while (L->next != NULL)
{
DeleteNextDNode(L);
}
free(L);
L = NULL;
}
int main()
{
DLinkList L;
InitDLinkList(L);
}