循环链表就是将尾结点的指针指向头结点形成一个循环的链表。循环单链表的初始化就是使头结点自己指向自己,而循环双链表的初始化是头结点的前后驱结点都是指向自己。循环链表的好处就是可以在插入或者删除表尾元素的操作时,不用再判断结点为空的可能,大大的减少了代码的复杂性。详情请看代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

//定义循环单链表
typedef struct Node{
	int data;
	struct Node *next;
}Node,*RepeatSimpleList;

//定义循环双链表
typedef struct DNode{
	int data;
	struct DNode *prior,*next;
}DNode,*RepeatDoubleList;

//初始化一个循环双链表 
bool InitRepeatDoubleList(RepeatDoubleList &L){
	L = (DNode*)malloc(sizeof(DNode));
	if(L==NULL){
		return false;
	}
	L->prior = L;//头结点的前驱指针指向头结点 
	L->next = L;//头结点的后驱指针指向头结点 
}

//初始化一个循环单链表
bool InitRepeatSimpleList(RepeatSimpleList &L){
	L = (Node*)malloc(sizeof(Node));
	if(L==NULL){
		return false;
	}
	L->next = L;//头结点指向头结点 
	return true;
} 

//判断循环双链表是否为空
bool Empty(RepeatDoubleList L){
	if(L->next==L){
		return true;
	}else{
		return false;
	}
}


//判断循环单链表是否为空
bool isEmpty(RepeatSimpleList L){
	if(L->next==L){
		return true;
	}else{
		return false;
	}
}

//在p结点之后插入s结点(循环双链表)
bool InsertNextDNode(DNode *p,DNode *s){
	s->next = p->next;
	p->next->prior = s;
	s->prior = p;
	p->next = s;
} 

//删除p结点后的后继结点(循环双链表) 
bool DeleteNextDNode(DNode *p){
	DNode *q = p->next;//q指针指向目标结点 
	p->next = q->next;
	q->next->prior = p;	
	free(q);//释放目标结点 
}

//判断结点p是否为循环双链表尾结点
bool Finish(RepeatDoubleList L,DNode *p){
	if(p->next==L){
		return true;
	}else{
		return false;
	}
} 

//判断结点p是否为循环单链表尾结点
bool isFinish(RepeatSimpleList L,Node *p){
	if(p->next==L){
		return true;
	}else{
		return false;
	}
} 

int main(int argc, char** argv) {
	RepeatSimpleList L;//声明一个循环单链表
	RepeatDoubleList L;//声明一个循环双链表
	InitRepeatSimpleList(L);//初始化循环单链表 
	InitRepeatDoubleList(L);//初始化循环双链表 
	return 0;
}