主题:关于循环链表的问题
[size=5]下面是我用链表来实现:猴子选大王的问题,但不知问题出在哪了?编译总是通不过,希望高人能够指点一下,谢谢啦![/size]
// Note:Your choice is C++ IDE
#include <iostream>
using namespace std;
typedef struct pnode
{
int serial;
struct pnode *next;
}Linklist;
void init(Linklist *list0);
void prif(Linklist *list0);
void insearch(Linklist *list0,Linklist *rear0);
int main()
{
Linklist *list,*rear;
list=(Linklist *)malloc(sizeof(Linklist));
list->serial=1;
list->next=list;
init(list);
rear=prif(list);
insearch(list,rear);
return 0;
}
void init(Linklist *list0)
{
Linklist *p,*rear;
p=list0;
for(int i=2;i<=60;++i)
{
rear=(Linklist *)malloc(sizeof(Linklist));
rear->serial=i;
rear->next=p;
p->next=rear;
p=rear;
free(rear); //要不要释放?
}
return ;
}
Linklist * prif(Linklist *list0)
{
Linklist *p0;
p0=list0;
while(p0->next!=list0)
{
cout<<p0->serial<<"\t";
p0=p0->next;
}
cout<<p0->serial<<endl;
return p0;
}
void insearch(Linklist *list0,Linklist *rear0)
{
int count=0;
int n=0;
Linklist *p,*pro;
p=list0;
pro=rear0;
for(;count++;p=p->next)
{
if(n==59)
break;
else if(count==3)
{
++n;
cout<<"The "<<n<<" one is"<<p->serial<<"\t";
pro->next=p->next;
p->next=NULL;
free(p); //这两行对不?
p=pro;
count=0;
}
else
{
pro=pro->next;
}
}
cout<<"monkey leader is:\t"<<p->serial<<endl;
}[email]zhenzhongyao@126.com[/email]
[fly]多谢耐心指导 O(∩_∩)O~[/fly]
// Note:Your choice is C++ IDE
#include <iostream>
using namespace std;
typedef struct pnode
{
int serial;
struct pnode *next;
}Linklist;
void init(Linklist *list0);
void prif(Linklist *list0);
void insearch(Linklist *list0,Linklist *rear0);
int main()
{
Linklist *list,*rear;
list=(Linklist *)malloc(sizeof(Linklist));
list->serial=1;
list->next=list;
init(list);
rear=prif(list);
insearch(list,rear);
return 0;
}
void init(Linklist *list0)
{
Linklist *p,*rear;
p=list0;
for(int i=2;i<=60;++i)
{
rear=(Linklist *)malloc(sizeof(Linklist));
rear->serial=i;
rear->next=p;
p->next=rear;
p=rear;
free(rear); //要不要释放?
}
return ;
}
Linklist * prif(Linklist *list0)
{
Linklist *p0;
p0=list0;
while(p0->next!=list0)
{
cout<<p0->serial<<"\t";
p0=p0->next;
}
cout<<p0->serial<<endl;
return p0;
}
void insearch(Linklist *list0,Linklist *rear0)
{
int count=0;
int n=0;
Linklist *p,*pro;
p=list0;
pro=rear0;
for(;count++;p=p->next)
{
if(n==59)
break;
else if(count==3)
{
++n;
cout<<"The "<<n<<" one is"<<p->serial<<"\t";
pro->next=p->next;
p->next=NULL;
free(p); //这两行对不?
p=pro;
count=0;
}
else
{
pro=pro->next;
}
}
cout<<"monkey leader is:\t"<<p->serial<<endl;
}[email]zhenzhongyao@126.com[/email]
[fly]多谢耐心指导 O(∩_∩)O~[/fly]