【数据结构】线性表链式存储结构-循环链表
程序员文章站
2022-05-06 12:42:42
...
文章目录
- 循环单链表将尾结点next指针域指向头结点
- 循环双链表将尾结点next指针指向头结点,头结点的prior指针指向尾结点
- 可通过p->next ==L来判断尾结点,双向链表可以通过L->prior来找到尾结点
循环链表应用实例
//宏定义
#define FAILED 1
#define SUCCESS 0
typedef struct CirSLinkNode
{
int data;
struct CirSLinkNode* next;
}CirSLinkNode;
typedef struct CirDLinkNode
{
int data;
struct CirDLinkNode* next;
struct CirDLinkNode* prior;
}CirDLinkNode;
//带头结点的循环单链表,统计其data域值为x的结点个数
int CountCirSLinkList(CirSLinkNode* L,int x)
{
CirSLinkNode* p = L->next;
int i = 0;
while (p!=L)
{
if (p->data ==x)
i++;
p = p->next;
}
return i;
}
//删除双向循环链表中值为x的第一个元素
int DelCirDLinkList(CirDLinkNode* L,int x)
{
CirDLinkNode* p = L->next;
while (p!=L &&p->data !=x)
p = p->next;
if (p == L) FAILED;
p->next->prior = p->prior;
p->prior->next = p->next;
free(p);
return SUCCESS;
}
//判断带头结点的双向循环链表(带有至少两个结点)是否对称
int SymCirDLinkList(CirDLinkNode* L)
{
CirDLinkNode* h = L->next;
CirDLinkNode* r = L->prior;
int isSame = 1;
while (isSame)
{
if (h->data != r->data)
isSame = 0;
else
{
if ((h->next == r->prior) || (h == r->prior)) break;
h = h->next;
r = r->prior;
}
}
return isSame;
}