#include
using namespace std;
typedef struct DNode {
int data;
struct DNode* next, * perior;
}DNode,DLinkList;
//初始化带头结点的双链表
bool InitDLinkList(DLinkList& L) {
DNode L = new DNode;
if (L == NULL) //内存分配失败
return false;
L->next = NULL;
L->perior = NULL;
return true;
}
//判断双链表是否为空
bool Empty(DLinkList L) {
return (L == NULL);
}
//在p结点后插入s结点
bool InsertNextDNode(DNode* p, DNode* s){
if (p == NULL || s == NULL)
return false;
s->next = p->next;
if (p->next == NULL)
s->perior = p;
p->next->perior = s;
p->next = s;
return true;
}
//删除p结点后的后继结点
bool 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->perior = p;
delete(q);
return true;
}
//循环释放各数据结点
void DestoryList(DLinkList &L) {
while (L->next != NULL)
DeleteNextDNode(L);
delete(L); //释放头结点
L == NULL;
}
//遍历双链表
while (p != NULL) {
cout << p->data;
p = p->next;
}
while (p != NULL) {
cout << p->data;
p = p->perior;
}
while (p->perior != NULL) { //跳过头结点
cout << p->data;
p = p->perior;
}
c++ 双链表的初始化,插入,删除,遍历
最新推荐文章于 2023-03-30 23:50:15 发布