数据结构链表之单循环链表

在这里,也设置一个头指针。

对于有着头指针的单循环链表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 =
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值