约瑟夫环 出列顺序中遇到的free()函数有关问题
约瑟夫环 出列顺序中遇到的free()函数问题
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int a;
int b;
struct node *next;
}Lnode;
Lnode *creatlist(int n);
void printlist(Lnode *p);
void fun(Lnode *p,int n,int m);
main()
{ Lnode *p;
int n,m;
printf("input n and m:\n");
scanf("%d,%d",&n,&m);
p=creatlist(n);
printf("the list is:\n");
printlist(p);
printf("\nchu lie sunxu:\n");
fun(p,n,m);
}
Lnode *creatlist(int n)
{ int i;
Lnode *head,*p,*s;
head=( Lnode *) malloc(sizeof(Lnode ));
p=head;
head->next=p;
printf("enter a,b:\n");
scanf ("%d,%d",&p->a,&p->b);
for (i=2; i<=n; ++i )
{ s=(Lnode *) malloc(sizeof(Lnode ));
scanf ("%d,%d",&s->a,&s->b);
s->next=head;
p->next=s; p=s;
}
return (s);
}
void printlist(Lnode *p)
{ Lnode *q;
q=p->next;
while(q!=p)
{ printf("%3d",q->b);
q=q->next;
}
printf("%3d",p->b);
}
void fun(Lnode *p,int n,int m)
{ Lnode *q;
int i,count;
q=p;
for(i=1;i<=n;i++)
{ count=0;
while(count!=m)
{ p=q;
q=q->next;
count++;
}
printf("%3d",q->a);
m=q->b;
p->next=q->next; free(q);
}
}
------解决方案--------------------
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int a;
int b;
struct node *next;
}Lnode;
Lnode *creatlist(int n);
void printlist(Lnode *p);
void fun(Lnode *p,int n,int m);
main()
{ Lnode *p;
int n,m;
printf("input n and m:\n");
scanf("%d,%d",&n,&m);
p=creatlist(n);
printf("the list is:\n");
printlist(p);
printf("\nchu lie sunxu:\n");
fun(p,n,m);
}
Lnode *creatlist(int n)
{ int i;
Lnode *head,*p,*s;
head=( Lnode *) malloc(sizeof(Lnode ));
p=head;
head->next=p;
printf("enter a,b:\n");
scanf ("%d,%d",&p->a,&p->b);
for (i=2; i<=n; ++i )
{ s=(Lnode *) malloc(sizeof(Lnode ));
scanf ("%d,%d",&s->a,&s->b);
s->next=head;
p->next=s; p=s;
}
return (s);
}
void printlist(Lnode *p)
{ Lnode *q;
q=p->next;
while(q!=p)
{ printf("%3d",q->b);
q=q->next;
}
printf("%3d",p->b);
}
void fun(Lnode *p,int n,int m)
{ Lnode *q;
int i,count;
q=p;
for(i=1;i<=n;i++)
{ count=0;
while(count!=m)
{ p=q;
q=q->next;
count++;
}
printf("%3d",q->a);
m=q->b;
p->next=q->next; free(q);
}
}
------解决方案--------------------
- C/C++ code
//假设有n个人团团围做,从第1个人开始数数,数到第m个人时候,第m个人出列, //然后继续从1开始数数,数到第m个人退出 #include <stdio.h> #include <conio.h> int i,k,t; int n,m; static char f[1001];//0该座位未出圈,1该座位已出圈 void main() { while (1) { printf("Input n m(1000>=n>=m>=1):"); fflush(stdout); rewind(stdin); if (2==scanf("%d%d",&n,&m)) { if (1000>=n && n>=m && m>=1) break; } } t=0;//已出圈总人数 i=1;//座位编号 k=1;//当前要数的数 while (1) { if (0==f[i]) { if (m==k) { t++; f[i]=1; printf("%3d ",i); if (0==t%10) printf("\n"); if (t>=n) break; } k++;if (k>m) k=1; } i++;if (i>n) i=1; } cprintf("Press any key ..."); getch(); }
------解决方案--------------------
#include"stdio.h"
#include<stdlib.h>
typedef struct node{
int b;//结点位序
struct node *next;
}Lnode,LinkList;
LinkList *creatlist(int n)
{
int i;
LinkList *head,*s,*tail;
head=( Lnode *)malloc(sizeof(Lnode));
head = NULL;
for (i=1;i<=n;i++)
{
s=(LinkList *)malloc(sizeof(Lnode ));
if (s == NULL)
{
printf("申请失败\n");
}
s->b = i;
if (head == NULL)
{
head = s;
tail = head;
}
else
{
tail->next = s;
tail = s;
}
tail->next = head;
}
return head;
}
void fun(LinkList *head,int n,int m)
{
LinkList *q,*pre;
int count=0;
int num = 0;//记录已经出列的人数
q=head;
while(num != n)
{
if (count!=m)
{
pre = q;
q=q->next;
count++;
}
count = 0;
printf("%3d",q->b);