解决链表实现 下面探讨一下单向循环链表的问题。
约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。
依据链表的实现方式实现了单循环链表:代码如下:
头文件:
#ifndef _CIRCLELIST_H_
#define _CIRCLELIST_H_
typedef void CircleList;
typedef struct _tag_circlrlistnode
{
struct _tag_circlrlistnode* next;
}ListNode;
CircleList* CircleList_Create();
void CircleList_Clear(CircleList* mlist);
void CircleList_Destroy(CircleList* mlist);
int CircleList_Length(CircleList* mlist);
int CircleList_Insert(CircleList* mlist,ListNode* node,int pos);
ListNode* CircleList_Delete(CircleList* mlist,int pos);
ListNode* CircleList_Get(CircleList* mlist,int pos);
//游标的处理
ListNode* CircleList_SliderCurrent(CircleList* mlist);
ListNode* CircleList_SliderNext(CircleList* mlist);
ListNode* CircleList_DeleteNode(ListNode* node);
ListNode* CircleList_SliderReset(CircleList* mlist);
#endif<span style="font-family: Arial; background-color: rgb(255, 255, 255);"> </span>
头文件的实现部分:
#include "stdlib.h"
#include "stdio.h"
#include "mycirclelist.h"
typedef struct _tag_TCirclelist
{
ListNode header;
ListNode* slider;
int length;
}TCircleList;
CircleList* CircleList_Create()
{
TCircleList* tcirclelist = (TCircleList*)malloc(sizeof(TCircleList));
if(tcirclelist == NULL)
{
return NULL;
}
tcirclelist->header.next = NULL;
tcirclelist->length = 0;
tcirclelist->slider = NULL;
return tcirclelist;
}
void CircleList_Clear(CircleList* mlist)
{
TCircleList* tcirclelist = (TCircleList*)mlist;
if(tcirclelist == NULL)
{
return;
}
tcirclelist->length = 0;
tcirclelist->header.next = NULL;
tcirclelist->slider = NULL;
}
void CircleList_Destroy(CircleList* mlist)
{
TCircleList* tcirclelist = (TCircleList*)mlist;
if(tcirclelist == NULL)
{
return;
}
tcirclelist->length = 0;
tcirclelist->header.next = NULL;
tcirclelist->slider = NULL;
free(tcirclelist);
}
int CircleList_Length(CircleList* mlist)
{
TCircleList* tcirclelist = (TCircleList*)mlist;
if(tcirclelist == NULL)
{
return 0;
}
return tcirclelist->length;
}
int CircleList_Insert(CircleList* mlist,ListNode* node,int pos)
{
int i = 0;
ListNode* current = NULL;
ListNode* last = NULL;
//循环链表的插入与普通链表的插入不同之处就是头位置的插入
TCircleList* tcirclelist = (TCircleList*)mlist;
if(tcirclelist == NULL || pos < 0 || node == NULL)
{
return -1;
}
current = &(tcirclelist->header);
for(i = 0 ; i < pos; i++)
{
current = current->next;
}
//普通插入节点
node->next = current->next;
current->next = node;
//添加第一个元素 设置游标的位置
if( CircleList_Length(tcirclelist) == 0)
{
tcirclelist->slider = node;
}
//长度加1
tcirclelist->length++;
if(current == &(tcirclelist->header))
{
last = CircleList_Get(tcirclelist,CircleList_Length(tcirclelist)-1);
}
//获得最后一个节点的位置
if(last != NULL)
{
last->next = current->next;
}
return 0;
}
ListNode* CircleList_Delete(CircleList* mlist,int pos)
{
int i = 0;
ListNode* current = NULL;
ListNode* ret = NULL;
ListNode* last = NULL;
//循环链表的插入与普通链表的插入不同之处就是头位置的插入
TCircleList* tcirclelist = (TCircleList*)mlist;
if(tcirclelist == NULL || pos < 0 )
{
return NULL;
}
current = &(tcirclelist->header);
for(i = 0 ; i < pos; i++)
{
current = current->next;
}
//删除头结点的处理
if(current == &(tcirclelist->header))
{
last = CircleList_Get(tcirclelist,CircleList_Length(tcirclelist)-1);
}
//删除节点
ret = current->next;
current->next = ret->next;
tcirclelist->length--;
//获得最后一个节点的位置
if(last != NULL)
{
last->next = current->next;
}
//如果删除的元素是游标指向的元素
if(tcirclelist->slider == ret)
{
tcirclelist->slider = ret->next;
}
//如果链表只有一个节点 进行容错处理
if( CircleList_Length(tcirclelist) == 0 )
{
tcirclelist->header.next = NULL;
tcirclelist->slider = NULL;
}
return ret;
}
ListNode* CircleList_Get(CircleList* mlist,int pos)
{
int i = 0;
ListNode* current = NULL;
TCircleList* tcirclelist = (TCircleList*)mlist;
if(tcirclelist == NULL || pos < 0 || pos > CircleList_Length(tcirclelist))
{
return NULL;
}
current = &(tcirclelist->header);
for(i = 0 ; i < pos;i++)
{
current = current->next;
}
return current->next;
}
//游标的处理
ListNode* CircleList_SliderCurrent(CircleList* mlist)
{
TCircleList* tcirclelist = (TCircleList*)mlist;
if(tcirclelist == NULL)
{
return NULL;
}
return tcirclelist->slider;
}
ListNode* CircleList_SliderNext(CircleList* mlist)
{
//游标下移 注意游标默认值是第一个元素节点 返回的是下移之前的位置
ListNode* ret;
TCircleList* tcirclelist = (TCircleList*)mlist;
if(tcirclelist == NULL)
{
return NULL;
}
ret = tcirclelist->slider ;
tcirclelist->slider = tcirclelist->slider->next;
return ret;
}
ListNode* CircleList_DeleteNode(CircleList* mlist,ListNode* node)
{
TCircleList* sList = (TCircleList*)mlist;
ListNode* ret = NULL;
int i = 0;
if( sList != NULL )
{
ListNode* current = (ListNode*)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;
}
ListNode* CircleList_SliderReset(CircleList* mlist)
{
TCircleList* tcirclelist = (TCircleList*)mlist;
if(tcirclelist == NULL)
{
return NULL;
}
tcirclelist->slider = tcirclelist->header.next;
return tcirclelist->slider;
}
模仿约瑟夫环的场景实现:
#include "stdlib.h"
#include "stdio.h"
#include "mycirclelist.h"
typedef struct _tag_Teacher
{
ListNode node;
int age;
char* name;
}Teacher;
void testdemo1()
{
CircleList* mcirclelist;
int i = 0;
Teacher t1,t2,t3,t4,t5,t6,t7,t8;
t1.age = 11;
t2.age = 22;
t3.age = 33;
t4.age = 44;
t5.age = 55;
t6.age = 66;
t7.age = 77;
t8.age = 88;
t1.name = "shao11";
t2.name = "shao22";
t3.name = "shao33";
t4.name = "shao44";
t5.name = "shao55";
t6.name = "shao66";
t7.name = "shao77";
t8.name = "shao88";
mcirclelist = CircleList_Create();
CircleList_Insert(mcirclelist,(ListNode*)&t1,0);
CircleList_Insert(mcirclelist,(ListNode*)&t2,1);
CircleList_Insert(mcirclelist,(ListNode*)&t3,2);
CircleList_Insert(mcirclelist,(ListNode*)&t4,0);
CircleList_Insert(mcirclelist,(ListNode*)&t5,4);
CircleList_Insert(mcirclelist,(ListNode*)&t6,0);
CircleList_Insert(mcirclelist,(ListNode*)&t7,0);
CircleList_Insert(mcirclelist,(ListNode*)&t8,0);
for(i = 0; i < CircleList_Length(mcirclelist); i++)
{
Teacher* mnode = (Teacher*)CircleList_Get(mcirclelist,i);
printf("node age:%d;node name:%s\n",mnode->age,mnode->name);
}
//游标重置
CircleList_SliderReset(mcirclelist);
while( CircleList_Length(mcirclelist) > 0)
{
Teacher* mnode = NULL;
for(i = 1; i < 3;i++)
{
mnode = (Teacher*)CircleList_SliderNext(mcirclelist);
}
//删除节点 当前游标的位置
mnode = CircleList_SliderCurrent(mcirclelist);
printf("delete node age:%d;node name:%s\n",mnode->age,mnode->name);
CircleList_DeleteNode(mcirclelist,mnode);
}
CircleList_Destroy(mcirclelist);
}
void main()
{
testdemo1();
printf("邵忠棋");
system("pause");
}
delete node的意思就是出局的人。。。。
让你能够更加体会出数据结构的魅力, Just Like! Just Do IT!!