free()调用时,第二次调用会出现产生断点???(vs2019)求解,救救孩子吧!!!
#include<stdio.h>#include<string.h>
#include<stdlib.h>
#define MAX 1000
struct people
{
people* front;
int n;
people* next;
};
typedef people People;
void chushihua(People* a,int n);
int main()
{
People a;
People* p;
People* q,*p1,*p2;
a.next = NULL;
a.front = NULL;
a.n = 1;
int N,M,K;
int i=0, m,count=0;
int shunxu;
scanf_s("%d %d %d", &N, &M, &K);
chushihua(&a, N);
m = N;
q = &a;
p = &a;
while (m!=1)
{
count = 0;
while (count != M)
{
p = q;
q=q->front;
count++;
}
p = q->next;
printf("%p\n", p);
shunxu = p->n;
p2 = p->front;
p1 = p->next;
p1->front = p2;
p2->next = p1;
count = 0;
m--;
i++;
if (m != 1)
free(p);
if (m == 1)
{
break;
}
p = q;
while (count != K)
{
p = q;
q = q->next;
count++;
}
p = q->front;
printf("%p\n", p);
shunxu = p->n;
p2 = p->front;
p1 = p->next;
p1->front = p2;
p2->next = p1;
if(m!=1)
free(p);
i++;
m--;
}
shunxu = q->n;
for (i = 0; i < N; i++)
{
printf("%d ", shunxu);
}
return 0;
}
void chushihua(People* a, int n)
{
int j;
People* NEW;
People* other;
other = (People*)malloc(sizeof(people));
;
for (j = 0; j < n-2; j++)
{
NEW = (People*)malloc(sizeof(People));
NEW->front = NULL;
NEW->next = NULL;
if (a->next == NULL)
{
a->next = NEW;
NEW->front = a;
}
else
{
other->next = NEW;
NEW->front = other;
}
NEW->n = j+2;
other = NEW;
}
NEW = (People*)malloc(sizeof(People)+1);
NEW->front = other;
other->next = NEW;
NEW->next = a;
a->front = NEW;
NEW->n = n;
} 本帖最后由 jhq999 于 2021-12-14 11:33 编辑
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 1000
struct people
{
people* front;
int n;
people* next;
};
typedef people People;
void chushihua(People* a,int n);
int main()
{
People a;
People* p;
People* q,*p1,*p2;
a.next = NULL;
a.front = NULL;
a.n = 1;
int N,M,K;
int i=0, m,count=0;
int shunxu;
scanf_s("%d %d %d", &N, &M, &K);
chushihua(&a, N);
m = N;
q = &a;
p = &a;
while (m!=1)
{
count = 0;
while (count != M)
{
p = q;
q=q->front;
count++;
}
p = q->next;
printf("%p\n", p);
shunxu = p->n;
p2 = p->front;
p1 = p->next;
p1->front = p2;
p2->next = p1;
count = 0;
m--;
i++;
if (m != 1)
free(p);
if (m == 1)
{
break;
}
p = q;
while (count != K)
{
p = q;
q = q->next;
count++;
}
p = q->front;
printf("%p\n", p);
shunxu = p->n;
p2 = p->front;
p1 = p->next;
p1->front = p2;
p2->next = p1;
if(m!=1&&p!=&a)///////////////不可能释放已经声明变量a的内存空间
free(p);
i++;
m--;
}
shunxu = q->n;
for (i = 0; i < N; i++)
{
printf("%d ", shunxu);
}
return 0;
}
void chushihua(People* a, int n)
{
int j;
People* NEW;
People* other;
other = (People*)malloc(sizeof(people));
;
for (j = 0; j < n-2; j++)
{
NEW = (People*)malloc(sizeof(People));
NEW->front = NULL;
NEW->next = NULL;
if (a->next == NULL)
{
a->next = NEW;
NEW->front = a;
}
else
{
other->next = NEW;
NEW->front = other;
}
NEW->n = j+2;
other = NEW;
}
NEW = (People*)malloc(sizeof(People)+1);
NEW->front = other;
other->next = NEW;
NEW->next = a;
a->front = NEW;
NEW->n = n;
}
页:
[1]