单向循环链表
#include <iostream>
#include "stdio.h"
#include "string.h"
using namespace std;
typedef struct node
{
int data;
struct node *next;
}node;
node *Create(int n)
{
struct node *pRet = NULL;
if(0 != n)
{
int n_idx = 1;
node *p_node = NULL;
p_node = new node[n];
if(p_node == NULL)
{
return NULL;
}
else
{
memset(p_node,0,n*sizeof(node));
}
pRet = p_node;
while(n_idx<n)
{
p_node->data = n_idx;
p_node->next = p_node+1;
p_node = p_node->next;
n_idx++;
}
p_node->data = n;
p_node->next = pRet;
}
return pRet;
}
int main()
{
node *pList = NULL;
node *pIter = NULL;
int n = 20;
int m = 6;
pList = Create(n);
pIter = pList;
m%=n;
while(pIter != pIter->next)
{
int i = 1;
for(;i<m-1;i++)
{
pIter = pIter->next;
}
/*输出第m个节点的值*/
printf("%d\n",pIter->next->data);
/*从链表中删除第m个节点*/
pIter->next = pIter->next->next;
pIter = pIter->next;
}
printf("%d\n",pIter->data);
delete[] pList;
return 0;
}
6
12
18
4
11
19
7
15
3
14
5
17
10
8
2
9
16
13
1
20