//双链表的插入操作
//在p结点之后插入s结点
bool InsertNextDNode (DNode *p, DNode *s) {
if (p == NULL || s == NULL) { //非法参数
return false;
}
s -> next = p -> next;
if (p -> next != NULL) { //若p结点有后继结点
p -> next -> prior = s;
}
s -> prior = p;
p -> next = s;
return true;
}
//双链表的删除
//删除p结点的后继结点
bool DeleteNextDNode (DNode *p) {
if (p == NULL) {
return false;
}
DNode *q = p -> next; //找到p的后继结点q
if (q == NULL) {
return false; //p没有后继结点
}
p -> next = q -> next;
if (q -> next != NULL) { //q结点不是最后一个结点
q -> next -> prior = p;
}
free(q); //释放结点空间
return true;
}
//销毁一个双链表
void Destory (DLinklist &L) {
//循环释放各个数据结点
while (L -> next != NULL) {
DeleteNextDNode(L);
}
free(L); //释放头结点
L = NULL; //头结点指向NULL
}
双链表的遍历
//后向遍历
while (p != NULL) {
//对结点p作相应处理
p = p -> next;
}
//前向遍历
while (p != NULL) {
//对结点p做相应处理
p = p -> prior;
}
//前向遍历(跳过头结点)
while (p -> prior != NULL) {
//对结点p做相应处理
p = p -> prior;
}