循环链表
1.循环链表的设计
1.循环链表的结构设计
typedef struct CNode{
int data;
struct CNode* next;
}CNode, *CList;
2.循环链表的示意图:
3.循环链表和单链表的区别
- 唯一区别,没有空指针,尾节点的后继为头,为循环之意
二.循环链表的实现
bool Insert_head(CList plist, int val){
assert(plist!=NULL);
CNode*p = (CNode*)malloc(sizeof(CNode));
assert(p != NULL);
p->data = val;
p->next = plist->next;
plist->next = p;
return true;
}
bool Insert_tail(CList plist, int val){
assert(plist != NULL);
CNode*p = (CNode*)malloc(sizeof(CNode));
assert(p != NULL);
p->data = val;
CNode*q = plist;
for(; q->next != plist; q = q->next){
;
}
p->next = q->next;
q->next = p;
}
bool IsEmpty(CList plist){
return plist->next == plist;
}
bool DelVal(CList plist, int val){
assert(plist != NULL);
if(IsEmpty(plist)){
return false;
}
CNode*p = GetPrio(plist, val);
CNode*q = p->next;
p->next = q->next;
free(q);
return true;
}
CNode* Getprio(CList plist, int key){
assert(plist != NULL);
CNode*p = plist;
for(; p->next != plist; p = p->next){
if(p->next->data == key){
return p;
}
}
return NULL;
}
void Destroy(CList plist)
{
CNode* p;
while(plist->next != plist)
{
p = plist->next;
plist->next = p->next;
free(p);
}
3.循环链表的总结
- 循环链表其实和单链表是一样的操作,只是在处理的时候处理好尾节点即可,切记遍历循环链表中不可出现NULL,若遍历的时候出现NULL就错了。