# 双链表

1、建立双链表

/*头插法*/
{
L->prior = L->next = NULL;

for (int i = 0; i < n; i++)
{
s->data = a[i];
s->next = L->next;

if (L->next != NULL)
{
L->next->prior = s;
}

L->next = s;
s->prior = L;
}
}


/*尾插法*/
{
L->next = L->prior = NULL;
r = L;

for (int i = 0; i < n; i++)
{
s->data = a[i];
r->next = s;
s->next = NULL;
s->prior = r;
r = s;
}
r->next = NULL;
}


插入节点

bool ListInsert(DLinkNode*& L, int i, int e)
{
p = L;
int j = 0;

while (j < i - 1 && p != NULL)
{
p = p->next;
j++;

}
if (p == NULL)
return false;
else
{
s->data = e;
s->next = p->next;
if (p->next != NULL)
{
p->next->prior = s;
}
p->next = s;
s->prior = p;
return true;

}

}


删除节点

bool ListDelete(DLinkNode*& L, int i, int& e)
{
int j = 0;

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;
else
{
e = q->data;
p->next = q->next;
if (q->next != NULL)
{
p->next->prior = p;
}
free(q);
return true;
}
}

}


逆置

void reserse(DLinkNode*& L)
{
L->next = NULL;

while (p != NULL)
{
q = p->next;
p->next = L->next;
if (L->next != NULL)
{
L->next->prior = p;
}
L->next = p;
p->prior = L;
p = q;

}
}


递增

void sort(DLinkNode*& L)
{
DLinkNode* p, * q, * pre;
p = L->next->next;
L->next->next = NULL;

while (p != NULL)
{
pre = L;
q = p->next;

while (pre->next != NULL && pre->next->data < p->data)
{
pre = pre->next;

}
p->next = pre->next;
if (pre->next != NULL)
{
pre->next->prior = p;
}
pre->next = p;
p->prior = pre;

p = q;

}

}


• 0
点赞
• 0
收藏
• 0
评论
09-27
03-22
09-30
12-22
11-28
06-14
12-25
03-26

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助