DogHead1999 2018-12-03 11:21 采纳率: 0%
浏览 634

请问我这个程序为什么内存超限

#include
#include
struct linklist*create(int n);
int find(struct linklist*head,int n,int all);
void freememory(struct linklist*head);
struct linklist
{
int data;
struct linklist*next;
};
int main()
{
struct linklist*head;
int n,num,i;
int answer;
int group;
scanf("%d",&group);
for(i=0;i {
scanf("%d%d",&n,&num);
if(n>num)
{
head=create(n);
answer=find(head,num,n);
printf("%d\n",answer);
freememory(head);
}
else
printf("%d",n);
}
return 0;
}
struct linklist*create(int n)
{
int i;
struct linklist*p1,*p2,*head;
head=NULL;
p1=p2=malloc(sizeof(struct linklist));
for(i=0;i {
p1->data=i+1;
if(i==0)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=malloc(sizeof(struct linklist));
}
p2->next=head;
return head;
}
int find(struct linklist*head,int n,int all)
{
int count=0,temp=0,m;
struct linklist*p1,*p2;
p1=p2=head;
while(1)
{
if(p1==head)
count=0;
count++;
if(count%n==0)
{
temp++;
p2->next=p1->next;
free(p1);
p1=p2;

}
if(temp==all-n)
{
p1=p1->next;
m=p1->data;
return m;
}
p2=p1;
p1=p1->next;
}
}
void freememory(struct linklist*head)
{
struct linklist*p1,*p2;
p1=p2=head;
while(p1->next==head)
{
p1=p1->next;
}
p1->next=NULL;
p1=head;
while(p1!=NULL)
{
p2=p1;
p1=p1->next;
free(p2);
}
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器