📄 selemonkeyking.cpp
字号:
//建立循环链表,实现选猴王的程序
#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define LEN sizeof(struct LNode)
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*LinkList;
struct LNode *L;
void CreateList_L(LinkList &L,int n)
{
int i;
LinkList p;
L=(LinkList)malloc(sizeof(int)); /*Create the first node*/
L->next=NULL;
for(i=n; i>0; --i) /*Create the next node*/
{
p=(LinkList)malloc(sizeof(int));
p->data=i;
p->next = L->next;
L->next = p;
}
p=L->next; /*Creat circular linked list*/
for(i=0;i<n-1;i++)
p=p->next;
if(p->next==NULL) p->next=L->next;
}
LNode ListDelete_L( LNode *p,int i) /*Delete node of i */
{
int j=0;
while(p->next && j<i-1)
{
p=p->next;
++j;
}
p->next = p->next->next;
return *p;
}
void main()
{
LNode *p;
int i,n,m;
printf("Please input n and m\n");
scanf("%d %d",&n,&m);
CreateList_L(L,n);
p=L->next;
for(i=n;i>1;i--)
*p= ListDelete_L(p,m);
printf("The king is %d !!\n",L->next->data);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -