#include<bits/stdc++.h>#include"CircList.h"usingnamespace std;boolinitCircList(CircList& first){
first =(CircNode *)malloc(sizeof(CircNode));
first ->link = first;}voidCreatCircListR(CircNode *rear, DataType endTag){//first = (CircNode *)malloc(sizeof(CircNode));
DataType val; cin >> val;if(val == endTag)return;else{
CircNode *s =(CircNode *)malloc(sizeof(CircNode));
s ->data = val;
s ->link = rear ->link; rear ->link = s; rear = s;CreatCircListR(rear, endTag);}}boolInsert(CircList& first,int i, DataType x){//在第i个结点位置插入元素if(i <1)returnfalse;
CircNode *q = first;int k =0;while(k < i -1&& q ->link != first){
q = q ->link;
k ++;}if(k == i -1){
CircNode *p =(CircNode *)malloc(sizeof(CircNode));
p ->data = x;
p ->link = q ->link; q ->link = p;}else{returnfalse;}returntrue;}boolRemove(CircList& first,int i, DataType& x){if(i <1)returnfalse;
CircNode *q = first;int k =0;while(k < i -1&& q ->link != first){
q = q ->link;
k ++;}if(k == i -1&& q ->link != first){
q ->link = q ->link ->link;}else{returnfalse;}returntrue;}voidprintCircList(CircList& first){
CircNode *p;for(p = first ->link; p != first; p = p ->link){
cout << p ->data <<", ";}
cout << endl;}
循环链表示例约瑟夫问题
#include<bits/stdc++.h>#include"CircList.cpp"usingnamespace std;voidJosephus(CircList& first,int n,int m){
CircNode *p = first ->link,*pr =NULL;for(int i =0; i < n -1; i ++){for(int j =1; j < m; j ++){
pr = p; p = p ->link;if(p == first){pr = first; p = first ->link;}}
cout << p ->data <<", ";
pr ->link = p ->link;free(p);
p = pr ->link;if(p == first){pr = first; p = pr ->link;}}}intmain(){
CircList L =(CircNode *)malloc(sizeof(CircNode));initCircList(L);
DataType endTag =-1;int n, m;
cin >> n >> m;for(int i =1; i <= n; i++){Insert(L, i, i);}printCircList(L);Josephus(L, n, m);system("pause");}