在i位置插入元素
typedef int ElemType;
// 定义双向循环链表节点结构体
struct DcLnode
{
ElemType data;
DcLnode *prior, *next;
}* DcLinkList; // 定义双向循环链表头指针类型别名
// 初始化
void InitList(DcLinkList &L)
{
L = new DcLnode;
L->prior = L;
L->next = L;
}
// 在双向循环链表的指定位置插入元素
void InsertList(DcLinkList &L, const ElemType &e, const int &i)
{
DcLnode *p = new DcLnode;
p->data = e;
DcLnode *c = L; //定义一个指向头结点的指针c
for (int j = 0; j < i && c->next != L) //确保链表不为空
{
c = c->next;
++j;
}
if (i < 0 || c->next==L) {
cerr << "out of range"
}
p->next = c->next;
p->prior = c;
c->next->prior = p;
c->next = p;
}
查找某个元素e
int FindList(DcLinkList&L,ElemType&e)
{
DcLnode*p=L->next;
int c=1;
while(p)
{
if(p->data==e)
{
return c;
}
++c;
p=p->next;
}
cerr>>"not found";
}
查找i位置的元素
bool LocatList(DcLinkList &L,ELemType &e,int &i)
{
Lcnode*p=L->next;
int j=1;
while(p!=L&&j<i)
{
++j;
p=p->next;
}
if(i<0||p==L)
{
cerr>>"out of range";
return false;
}
e=p->data;
return true;
}
删除某个元素
void DeleteList(DcLinkList &L,int &i)
{
Lcnode*p=L;
int j=0;
if(i<0)
{
cerr>>"out of range";
}
while(p!=L&&j<i-1)
{
++j;
p=p->next;
}
if(p==L)
{
cerr>>"out of range";
}
Lcnode*q=p->next;
p->next=q->next;
delete q;
}