#include<iostream>
using namespace std;
/*
*节点类
*/
struct DCNode
{
int data;
DCNode * prior;
DCNode * next;
};
/*
*链表类
*/
struct DCList
{
DCList()
{
size = 0;
}
size_t size;//记录节点数的个数
DCNode * head;//指向链表的头结点
};
/*
*分配一个节点,并给该节点的数据域赋值,默认值为0;
*/
DCNode * MakeNode(int t=0)
{
DCNode * p = (DCNode *)malloc(sizeof(DCNode));
p->data = t;
return p;
}
//初始化一个空的双向循环链表
void InitDCList(DCList &list)
{
//分配一个头结点
DCNode * p = MakeNode();
list.head = p;
list.size = 0;
p->next = p;
p->prior = p;
}
//释放所指向的节点
void FreeNode(DCNode * p)
{
delete p;
}
//清空一个线性表
void clear(DCList &list)
{
DCNode * p = list.head;
p = p->next;
while(p!= list.head)
{
DCNode * p1 = p->next;
delete p;
p = p1;
}
list.size = 0;
}
//将s所指向的节点插入到链表的第一个节点之前
bool InsFirst(DCList &list,DCNode * s)
{
s->next = list.head->next;
list.head->next->prior = s;
list.head->next = s;
s->prior = list.head;
list.size++;
return true;
}
//删除链表中的第一个节点并以q指针返回
bool DelFirst(DCList &list,DCNode * & q)
{
if(list.head->next==list.head)//如果链表为空
{
q = 0;
return false;
}
q = list.head->next;
list.head->next->next->prior = list.head;
list.head->next = list.head->next->next;
list.size--;
return true;
}
//将s所指向的节点串接在链表的最后一个节点之后
bool Append(DCList &list,DCNode * s)
{
s->prior = list.head->prior;
s->next = list.head;
list.head->prior->next = s;
list.head->prior = s;
list.size++;
return true;
}
//删除链表中的尾节点,并以指针q返回
bool Re
双向循环链表C++实现(完整版)
本文详细介绍了如何使用C++实现双向循环链表,包括链表类和节点类的定义,以及一系列操作链表的方法,如初始化、插入、删除、遍历等。通过实例演示了链表的操作过程。
摘要由CSDN通过智能技术生成