数据结构C语言 约瑟夫环 急!!
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct LinkList{
int num;
int code;
struct LinkList *next;
}L;//建立链表的结点结构
void destroy(struct LinkList *r)
{
struct LinkList *p1,*q1;
p1=r;
while (p1!=NULL)
{
q1 = p1 -> next;
if(q1==r)
q1=NULL;
free(p1);
p1=q1;
}
r=NULL;
}
int main()
{
struct LinkList *L;
int i,n1,n2,n3,b,m;
struct LinkList *p,*q,*r;
L=NULL;
printf("请输入总人数:\n");
scanf("%d",&n1);
for(i=1;i<=n1;i++)
{
p=(struct LinkList*)malloc(sizeof(struct LinkList));//创建空间
p -> num = i;
printf("第%d个人的密码是:\n", i);
scanf("%d",&n2);
p -> code = n2;
if(L==NULL)
{
L=p;
q=p;
}
else
{
q -> next = p;
q = q -> next;
}
}
q -> next = L;
printf("密码信息如下:\n");
r=L;
for(i=1;i<=n1;i++)
{
printf("%7d",r -> code);
r = r -> next;
}
printf("\n");
printf("进入下一步请输入1:\n");
scanf("%d",&n3);
if(n3==1)
{
system("cls");
printf("请输入m值:\n");
scanf("%d",&m);
p=L;
while(n1!=1)
{
if(b==m)
{
printf("%7d",p -> num);
m = p -> code;
q = p;
p = p -> next;
destroy(q);
b=1;
n1--;
}
else
{
p = p -> next;
b++;
}
}
if(n1==1)
{
printf("%7d",p -> num);
}
}
return 0;
} 展开
给你一个参考一下:
程序源代码:
#include<stdio.h>
typedef struct Node
{
int num;
int pasword;
struct Node *next;
}LinkList;
LinkList *creat(int n)
{
LinkList *p,*q,*head;
int i=1;
head=p=(LinkList *)malloc(sizeof(LinkList));
p->num=i;
printf("请输入第1个人的密码:");
scanf("%d",&p->pasword);
for(i=2;i<=n;i++)
{
q=(LinkList *)malloc(sizeof(LinkList));
if(q==0) return(0);
printf("请输入第%d个人的密码:",i);
scanf("%d",&q->pasword);
q->num=i;
p->next=q;
p=q;
}
p->next=head; /*使链表尾指向链表头形成循环链表*/
return head;
}
void fun(LinkList *L)
{
int m,i;
LinkList *p=L,*q,*s;
printf("请输入m的初值:");
scanf("%d",&m);
printf("出列顺序为:");
while(p->next!=p)
{
for(i=1;i<m;i++)
{ q=p;
p=p->next;
}
printf("%5d",p->num);
m=p->pasword;
s=p;
q->next=p->next;
p=p->next;
free(s);
}
printf("%5d",p->num);
printf("\n");
}
main()
{ LinkList *L;
int n;
printf("请输入实验人数:");
scanf("%d",&n);
L=creat(n);
fun(L);
}
# include <stdio.h>
# include <malloc.h>
struct stu
{
int num;
struct stu *next;
};
int m,n;struct stu *p,*q,*s;
struct stu *create()
{
struct stu *head,*s1,*r;
int i;
head=NULL;
for(i=1;i<=n;i++)
{
s1=(struct stu *)malloc(sizeof(struct stu));
s1->num =i;
if(head==NULL)head=s1;
else r->next =s1;
r=s1;
}
if(r) r->next =head;
return head;
}
struct stu * get_index(struct stu *head,int k)
{
int j=1;
if (k<1)
{
printf ("输入错误\n");
return NULL;
}
s=head;
while (s && j<k)
{
s=s->next ;j++;
}
return s;
}
void f1 (struct stu *head,struct stu *h)
{
int x,i;
x=m/2;
p=h;
i=1;printf ("依次出列的人的编号为:");
while (p->next!=p)
{
q=p->next;
for (i=1;i<x;i++)
{p=q->next;q=p->next;}
p->next =q->next ;
printf ("%d ",q->num);
free(q);
p=p->next ;
}
printf ("最后剩下的人的编号为%d\n",p->num );
}
void f2 (struct stu *head,struct stu *h)
{
int i,x;
x=m/2;
i=1;
p=h;
printf ("依次出列的人的编号为:");
do
{
for (i=1;i<=x;i++)
{q=p->next ;p=q->next ;}
q->next =p->next ;
printf ("%d ",p->num);
free(p);
p=q->next ;
}
while (q->next!=q);
printf ("\n最后剩下的人的编号为%d\n",q->num);
}
void main ()
{
int k;
struct stu *head,*h;
printf ("请输入总人数:");
scanf ("%d",&n);
printf ("请输入退出圈子的人的编号:");
scanf("%d",&m);
printf ("请输入开始报数的人的编号:");
scanf ("%d",&k);
head=create ();
h=get_index(head,k);
if (m%2==0) f1(head,h);
else f2(head,h);
}
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct LNode{
int number;
struct LNode *next;
} LNode,*Linklist;
int main()
{
int M,N;
int i;
Linklist L,r;
printf("Please input two M , N:");
scanf("%d%d",&M,&N);
L = (Linklist)malloc(N * sizeof(struct LNode));
if(L == NULL) printf("Error!\n"),exit(0);
r = L;
for(i = 1;i < N;i++)
{
r->next = L + i;
r->number = i;
r = r->next;
}
r->next = L;
r->number = N;
while(r != r->next)
{
for(i = 0;i < M - 1;i++)
r = r->next;
printf("%4d",r->next->number);
r->next = r->next->next;
}
printf("\n When N = %d and M = %d,Josephus,hiding in position %d survive.\n",N,M,r->number);
free(L);
}
广告 您可能关注的内容 |