店小二酒价格表38度:舟--C++约瑟环问题

来源:百度文库 编辑:中科新闻网 时间:2024/04/28 23:50:58
以下程序的输出错误可能出在哪里呢?可能创建的时候出了问题,麻烦高手帮忙看一下.输入的时候请输入10,切勿输入别的数字.
主要是输出的那一个问题.
# include <iostream.h>
# include <stdlib.h>
# define NUll 0
struct ring
{
int num;
int key;
struct ring *next;
};

////////////////////////////////////////////////
struct ring *creat(int person)///////////////////////////
{
struct ring *head,*p1,*p2;
head=p1=p2=(struct ring *)malloc(sizeof(struct ring));
head->next=NULL;

//head->key=head->num=1;

for(int i=1;i<=person;i++)
{
p2=(struct ring *)malloc(sizeof(struct ring));
p2->num=i;
p2->key=i+2;
p1->next=p2;
p1=p2;
}

p1->next=head;
return head;
}
////////////////////////////////////////////////
void print(struct ring *head,int person)///////////////////
{
cout<<"创建的约瑟环是(编号,密码)"<<endl;
for(int i=1;i<=person;i++)
{
cout<<head->num<<" "<<head->key<<endl;
head=head->next;
}
}
////////////////////////////////////////////////
void huan(struct ring *head)
{
int floop=1;
struct ring *p=head;
while(floop)
{
int i=1;

while(i<p->key-1)
{
p=p->next;
i++;
}
struct ring *a;
a=p->next;
p->next=a->next;
cout<<a->num<<" "<<a->key<<endl;
p=p->next;
if(p->next==p)
floop=0;
free(a);
}

}
///////////////////////////////////////////////
int main()/////////////////////////////////////
{
struct ring *list;
int person;
cout<<"请输入参与的人数"<<endl;
cin>>person;

list=creat(person);//创建
print(list,person);//输出

// int start;//开始的删除数
// cout<<"请输入第一次想删除的数"<<endl;
// cin>>start;

cout<<"约瑟夫环的结果为"<<endl;
huan(list);//输入循环体

return 0;
}
有两个指针(*a,*p)指向同一个地址,这个地址存储的是一个结构体,如果*P的数据发生改变 ,那么输出*a的数据是不是*p改变的数值呢?还是原来分配地址时默认的数据!
我的程序可能错在这里,麻烦高手看一下!

# include <iostream.h>
# include <stdlib.h>
# define NUll 0
struct ring
{
int num;
int key;
struct ring *next;
};

////////////////////////////////////////////////
struct ring *creat(int person)///////////////////////////
{
struct ring *head,*p1,*p2;
head=p1=p2=(struct ring *)malloc(sizeof(struct ring));
head->next=NULL;

//head->key=head->num=1;

for(int i=1;i<=person;i++)
{
p2=(struct ring *)malloc(sizeof(struct ring));
p2->num=i;
p2->key=i+2;
p1->next=p2;
p1=p2;
}

p1->next=head;
return head;
}
////////////////////////////////////////////////
void print(struct ring *head,int person)///////////////////
{
head = head->next;
cout<<"创建的约瑟环是(编号,密码)"<<endl;
for(int i=1;i<=person;i++)
{
cout<<head->num<<" "<<head->key<<endl;
head=head->next;
}
}
////////////////////////////////////////////////
void huan(struct ring *head)
{
int floop=1;
struct ring *p=head->next;
while(floop)
{
int i=1;

while(i<p->key-1)
{
p=p->next;
i++;
}
struct ring *a;
a=p->next;
p->next=a->next;
cout<<a->num<<" "<<a->key<<endl;
p=p->next;
if(p->next==p)
floop=0;
free(a);
}

}
///////////////////////////////////////////////
int main()/////////////////////////////////////
{
struct ring *list;
int person;
cout<<"请输入参与的人数"<<endl;
cin>>person;

list=creat(person);//创建
print(list,person);//输出

// int start;//开始的删除数
// cout<<"请输入第一次想删除的数"<<endl;
// cin>>start;

cout<<"约瑟夫环的结果为"<<endl;
huan(list);//输入循环体

return 0;
}