循环链表
1、基本概念
循环链表的定义:将单链表中最后一个数据元素的next指针指向第一个元素
循环链表拥有单链表的所有操作
创建链表
销毁链表
获取链表长度
清空链表
获取第pos个元素操作
插入元素到位置pos
删除位置pos处的元素
新增功能:游标的定义 |
在循环链表中可以定义一个“当前”指针,这个指针通常称为游标,可以通过这个游标来遍历链表中的所有元素。 |
循环链表新操作对比单链表
获取当前游标指向的数据元素
将游标重置指向链表中的第一个数据元素
将游标移动指向到链表中的下一个数据元素
直接指定删除链表中的某个数据元素
CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node);
CircleListNode* CircleList_Reset(CircleList* list);
CircleListNode* CircleList_Current(CircleList* list);
CircleListNode* CircleList_Next(CircleList* list);
2、设计与实现
插入元素的分析 1) 普通位置插入元素 2) 添加第一个元素(第一次插入元素) 3) 最后一个位置插入元素 4) 第一个位置插入元素 |
在第一个位置插入 |
删除元素
3、优点和缺点
优点:功能强了。
循环链表只是在单链表的基础上做了一个加强
循环链表可以完全取代单链表的使用
循环链表的Next和Current操作可以高效的遍历链表中的所有元素
缺点:
代码复杂度提高了
约瑟夫问题-循环链表典型应用
n 个人围成一个圆圈,首先第 1 个人从 1 开始一个人一个人顺时针报数,报到第 m 个人,令其出列。然后再从下一 个人开始从 1 顺时针报数,报到第 m 个人,再令其出列,…,如此下去,求出列顺序。
头文件
<span style="font-size:18px;">#ifndef _CIRCLELIST_H_
#define _CIRCLELIST_H_
typedef void CircleList;
typedef struct _tag_CircleListNode
{
struct _tag_CircleListNode * next;
}CircleListNode;
//创建循环链表
CircleList* CircleList_Create();
//销毁循环链表
void List_Destroy(CircleList* list);
//链表清空
void CircleList_Clear(CircleList* list);
//获取链表长度
int CircleList_Length(CircleList* list);
//插入结点
int CircleList_Insert(CircleList* list, CircleListNode* node, int pos);
//获得指定位置结点
CircleListNode* CircleList_Get(CircleList* list, int pos);
//删除指定位置结点
CircleListNode* CircleList_Delete(CircleList* list, int pos);
//add
//删除指定结点,并返回其地址
CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node);
//游标重置
CircleListNode* CircleList_Reset(CircleList* list);
//获得当前游标,返回
CircleListNode* CircleList_Current(CircleList* list);
//将当前游标地址返回,并且将游标移到下一个结点
CircleListNode* CircleList_Next(CircleList* list);
#endif</span>
实现.c
#include <stdio.h>
#include <malloc.h>
#include "CircleList.h"
typedef struct _tag_CircleList
{
CircleListNode header;
CircleListNode* slider;//游标
int length;
} TCircleList;
//创建头,用于存储信息。
CircleList* CircleList_Create() // O(1)
{
TCircleList* ret = (TCircleList*)malloc(sizeof(TCircleList));
if (ret == NULL)
{
return NULL;
}
ret->length = 0;
ret->header.next = NULL;
ret->slider = NULL;//建立游标
return ret;
}
//销毁链表
void CircleList_Destroy(CircleList* list) // O(1)
{
if (list == NULL)
{
return ;
}
free(list);
}
//链表清理
void CircleList_Clear(CircleList* list) // O(1)
{
TCircleList* sList = (TCircleList*)list;
if (sList == NULL)
{
return ;
}
sList->length = 0;
sList->header.next = NULL;
sList->slider = NULL;
}
//链表的长度
int CircleList_Length(CircleList* list) // O(1)
{
TCircleList* sList = (TCircleList*)list;
int ret = -1;
if (list == NULL)
{
return ret;
}
ret = sList->length;
return ret;
}
//节点插入
int CircleList_Insert(CircleList* list, CircleListNode* node, int pos) // O(n)
{
int ret = 0, i=0;
TCircleList* sList = (TCircleList*)list;
if (list == NULL || node== NULL || pos<0)
{
return -1;
}
//if( ret )
{
CircleListNode* current = (CircleListNode*)sList;//current初始化到头结点
for(i=0; (i<pos) && (current->next != NULL); i++)
{
current = current->next;
}
//current->next 0号节点的地址
node->next = current->next; //1
current->next = node; //2
//若第一次插入节点
if( sList->length == 0 )
{
sList->slider = node;//游标指向插入结点
}
sList->length++;
//若头插法
if( current == (CircleListNode*)sList )//如果current没有移动
{
//获取最后一个元素
CircleListNode* last = CircleList_Get(sList, sList->length - 1); //得到尾部元素
last->next = current->next; //3 将尾部指向头部
}
}
return ret;
}
//指定位置获得结点
CircleListNode* CircleList_Get(CircleList* list, int pos) // O(n)
{
TCircleList* sList = (TCircleList*)list;
CircleListNode* ret = NULL;
int i = 0;
if (list==NULL || pos<0)
{
return NULL;
}
//if( (sList != NULL) && (pos >= 0) && (sList->length > 0) )
{
CircleListNode* current = (CircleListNode*)sList;
for(i=0; i<pos; i++)
{
current = current->next;
}
ret = current->next;
}
return ret;
}
//指定位置删除结点
CircleListNode* CircleList_Delete(CircleList* list, int pos) // O(n)
{
TCircleList* sList = (TCircleList*)list;
CircleListNode* ret = NULL;
int i = 0;
if( (sList != NULL) && (pos >= 0) && (sList->length > 0) )
{
CircleListNode* current = (CircleListNode*)(sList);//指向头结点
CircleListNode* last = NULL;
for(i=0; i<pos; i++)
{
current = current->next;
}
//若删除第一个元素
if( current == (CircleListNode*)sList )
{
last = (CircleListNode*)CircleList_Get(sList, sList->length - 1);//last就是第0个节点
}
//求要删除的元素
ret = current->next;//保存删除节点地址
current->next = ret->next;
sList->length--;
//判断链表是否为空 如果last不等于NULL,
if( last != NULL ) //如果不为空
{
sList->header.next = ret->next; //next==NULL;
last->next = ret->next;//last=NULL
}
//若删除的元素为游标所指的元素
if( sList->slider == ret )
{
sList->slider = ret->next;
}
//若删除元素后,链表长度为0
if( sList->length == 0 )
{
sList->header.next = NULL;
sList->slider = NULL;
}
}
return ret;
}
CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node) // O(n)
{
TCircleList* sList = (TCircleList*)list;
CircleListNode* ret = NULL;
int i = 0;
if( sList != NULL )
{
CircleListNode* current = (CircleListNode*)sList;
//查找node在循环链表中的位置i
for(i=0; i<sList->length; i++)
{
if( current->next == node )
{
ret = current->next;
break;
}
current = current->next;
}
//如果ret找到,根据i去删除
if( ret != NULL )
{
CircleList_Delete(sList, i);
}
}
return ret;
}
//游标重置
CircleListNode* CircleList_Reset(CircleList* list) // O(1)
{
TCircleList* sList = (TCircleList*)list;
CircleListNode* ret = NULL;
if( sList != NULL )
{
sList->slider = sList->header.next;
ret = sList->slider;
}
return ret;
}
CircleListNode* CircleList_Current(CircleList* list) // O(1)
{
TCircleList* sList = (TCircleList*)list;
CircleListNode* ret = NULL;
if( sList != NULL )
{
ret = sList->slider;
}
return ret;
}
//返回游标指向的地址,并将游标向前移位
CircleListNode* CircleList_Next(CircleList* list) // O(1)
{
TCircleList* sList = (TCircleList*)list;
CircleListNode* ret = NULL;
if( (sList != NULL) && (sList->slider != NULL) )
{
ret = sList->slider;
sList->slider = ret->next;
}
return ret;
}
main函数
#include <stdio.h>
#include <stdlib.h>
#include "CircleList.h"
struct Value
{
CircleListNode header;
int v;
};
void main()
{
int i = 0;
CircleList* list = CircleList_Create();
struct Value v1, v2, v3, v4, v5, v6, v7, v8;
v1.v = 1; v2.v = 2; v3.v = 3; v4.v = 4;
v5.v = 5; v6.v = 6; v7.v = 7; v8.v = 8;
//强制类型转换
CircleList_Insert(list, (CircleListNode*)&v1, CircleList_Length(list));
CircleList_Insert(list, (CircleListNode*)&v2, CircleList_Length(list));
CircleList_Insert(list, (CircleListNode*)&v3, CircleList_Length(list));
CircleList_Insert(list, (CircleListNode*)&v4, CircleList_Length(list));
CircleList_Insert(list, (CircleListNode*)&v5, CircleList_Length(list));
CircleList_Insert(list, (CircleListNode*)&v6, CircleList_Length(list));
CircleList_Insert(list, (CircleListNode*)&v7, CircleList_Length(list));
CircleList_Insert(list, (CircleListNode*)&v8, CircleList_Length(list));
for(i=0; i<CircleList_Length(list); i++)
{
struct Value* pv = (struct Value*)CircleList_Next(list);
printf("%d\n", pv->v);
}
printf("\n");
//重置游标
CircleList_Reset(list);
while( CircleList_Length(list) > 0 )
{
struct Value* pv = NULL;
for(i=1; i<3; i++)
{
CircleList_Next(list);
}
pv = (struct Value*)CircleList_Current(list);
printf("%d\n", pv->v);
CircleList_DeleteNode(list, (CircleListNode*)pv);
}
CircleList_Destroy(list);
system("pause");
return ;
}