代码如下:
#include<iostream.h>
#include<malloc.h>
typedef struct lnode{
int position;
int password;
struct lnode *next;
}lnode,*linklist;
void creatlist(int n,linklist & p){
linklist t;
while(n>1){
t=(linklist)malloc(sizeof(lnode));
t->next=p->next;
p->next=t;
n--;
}
}
void inputpassword(int n,linklist & p){
int no,pw;
no=1;
do{
cin>>pw;
p->password=pw;
p->position=no;
no++;
cout<<p->position<<" "<<p->password;
}while(no<=n);
}
void gostar(int s,linklist & p){
while(s>1){
p=p->next;
s--;
}
}
void main(){
int n,s;
linklist p,t;
p=(linklist)malloc(sizeof(lnode));
p->next=p;
cout<<"input people number";
cin>>n;
creatlist(n,p);
cout<<"input "<<n<<" password";
inputpassword(n,p);
cout<<"input begin position";
cin>>s;
gostar(s,p);
cout<<"output";
s=p->password;
while(p!=p->next){
gostar(s,p);
cout<<p->position;//这里报错,
s=p->password;//谁知道怎么回事
t=p;
p=p->next;
free(t);
}
}
參考答案:inputpassword漏了p=p->next;
这样就相当于一直在给一个节点赋值
还有
while(p!=p->next){
gostar(s,p);
cout<<p->position;
s=p->password;
t=p;
p=p->next;//你只是将指针指到此节点后面的节点,但是没将前面的节点连起来,这样就无法构成封闭环了
free(t);
}