约瑟夫环 出列顺序中遇到的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);
}
}
[解决办法]
//假设有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);
pre->next = q->next;
num++;
free(q);
q = pre->next;
}
}
int main()
{
LinkList *head;
int n,m;
printf("input n and m:\n");
scanf("%d%d",&n,&m);
head=creatlist(n);//建立一个带头结点的链表
printf("\nchu lie sunxu:\n");
fun(head,n,m);
return 0;
}
改了点
[解决办法]
我也做了一个你看看
#include <stdio.h>#include <stdlib.h>/*约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。n=9,k=1,m=5【解答】出局人的顺序为5, 1, 7, 4, 3, 6, 9, 2, 8。*/struct list{ int n; struct list *netx;};typedef struct list List;int peo = 9,number = 1,out=5;void display(List *p);void outdisy(List *p);int main(){ int i; int counter;//技术器 List *head =NULL,*current,*p; for(i = 1;i <= peo;i++) { current =(List *)malloc(sizeof(List)); if(head == NULL) { head = current; current->n = i; } else { current->n = i; if(i == peo) current->netx = head; p->netx = current; } p = current; } printf("head地址:%x\n",head);// display(head); outdisy(head);//出列显示 printf("Hello world!\n"); return 0;}void display(List *p){ List *temp = p; do { printf("%d\t%x\n",temp->n,temp->netx); temp = temp->netx; }while(temp != p);}void outdisy(List *p){ int i; int count = 0,suni=1; List temp,*current; printf("out:\n"); while(count!= peo) { if(suni == 4) current = p; if(suni == out) { printf("%d\t%x\n",p->n,p->netx); temp = *p; free(p); p=temp.netx; current->netx=p; suni = 1; count++; } suni++; p = p->netx; }}