- #include "stdafx.h"
- #include <stdlib.h>
- #include <stdio.h>
- typedef struct node
- {
- int data;
- struct node* next;
- }LNode;
- LNode* Create(int n, int k) //创建循环链表
- {
- int start=k-1;
- if(start==0) start = n;
- LNode *s, *p, *L=NULL, *t;
- while(n!=0)
- {
- s = (LNode*)malloc(sizeof(LNode));
- if(L==NULL) p=s;
- if(n==start) t=s;
- s->data = n;
- s->next = L;
- L=s;
- n--;
- }
- p->next = L;
- return t;
- }
- LNode* GetNode(LNode *p)
- {
- LNode *q;
- for(q=p;q->next!=p;q=q->next);
- q->next = p->next;
- free (p);
- return (q);
- }
- int Print(LNode *p, int m)
- {
- int i;
- printf("出队编号:/n");
- while(p->next!=p)
- {
- for(i=1;i<=m;i++)
- {
- p=p->next;
- }
- printf("%3d",p->data);
- p = GetNode(p);
- }
- printf("%3d/n", p->data);
- return 0;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- LNode *p;
- int n,k,m;
- do
- {
- printf("输入总人数:");
- scanf("%d",&n);
- }while(n<0);
- do
- {
- printf("输入开始人的序号1~%d:",n);
- scanf("%d",&k);
- }while(k<=0||k>n);
- do
- {
- printf("输入间隔数字:");
- scanf("%d",&m);
- }while(m<=0);
- p=Create(n,k);
- Print(p,m);
- return 0;
- }
常见C语言面试题之十一:约瑟夫环
最新推荐文章于 2025-04-10 19:12:54 发布