双链表的操作
双链表的增删改```
typedef struct Dnode
{
ElemType data;
struct Dnode* prior;
struct Dnode* next;
}DLinkNode;
void creatlistf(DLinkNode*& L, ElemType a[], int n)
{
DLinkNode* s;
L = (DLinkNode*)malloc(sizeof(DLinkNode));
L->prior = L->next = NULL;
for (int i = 0; i < n; i++)
{
s = (DLinkNode*)malloc(sizeof(DLinkNode));
s->data = a[i];
s->next = L->next;
if (L->next != NULL)
L->next->prior = s;
L->next = s;
s->prior = L;
}
} //采用头插法建立双链表
void creatlistr(DLinkNode*& L, ElemType a[], int n)
{
DLinkNode* s, * r;
L = (DLinkNode*)malloc(sizeof(DLinkNode));
r = L;
for (int i = 0; i < n; i++)
{
s = (DLinkNode*)malloc(sizeof(DLinkNode));
s->data = a[i];
r->next = s;
s->prior = r;
r = s;
}
r->next = NULL;
} //采用尾插发建立双链表
bool listinsert(DLinkNode*& L, int i, ElemType e)
{
int j = 0;
DLinkNode* p = L, * s;
if (i < 0)return false;
while (j<i-1&&p!=NULL)
{
j++;
p = p->next;
}
if (p = NULL)return false;
else
{
s = (DLinkNode*)malloc(sizeof(DLinkNode));
s->data = e;
s->next = p->next;
if (p->next!=NULL)
p->next->prior = s;
s->prior = p;
p->next = s;
return true;
} //双链表插入操作
}
bool listdelete(DLinkNode*& L, int i, ElemType& e)
{
int j = 0;
DLinkNode* p = L, * q;
if (i < 0)return false;
while (j < i - 1 && p != NULL)
{
j++;
p = p->next;
}
if (p == NULL)
return false;
else
{
q = p->next;
if (q == NULL)
return false;
e = q->data;
p->next = q->next;
if (p->next!- NULL)
p->next->prior = p;
free(q);
return true;
}
}//双链表结点的删除
部分声明采用上一个博客单链表的声明