在这里,也设置一个头指针。
对于有着头指针的单循环链表L,判断L为空的条件为L->next == L,这是与单链表与循环链表不一样的地方。
除此之外,链表的最后一个节点指向头结点。
要注意的是,对循环链表进行操作的时候,需要注意链表为空的情况。
常用操作的代码为:
//循环列表
#include<stdlib.h>
#include<iostream>
struct List{
int val;
List *next;
};
List* initList( List **L )//make an empty List
{
*L = ( List* )malloc( sizeof( struct List ) );
(*L)->next = *L; //this is very important
return *L;
}
bool isEmpty( List* L)//test the list is an empty list or not
{
return L->next == L;
}
void insert( List* L, int x )
{
List* tmp = ( List* )malloc( sizeof( struct List ) );
if( tmp == NULL )
std::cerr << "MEMORY OUT!" << std::endl;
tmp->val = x;
if( L->next == L )//empty list
{
tmp->next = L;
L->next = tmp;
}
else
{
tmp->next = L->next;
L->next =