主题:[讨论]一道循环链队列的题
[size=4][color=FF0000]麻烦各位大侠,帮我改改,我书都看几遍了,不会改,谢谢!![/color][/size]
请将该题的回传方式改成用“引用回传”
#include<iostream.h>
typedef int DataType;
typedef struct node{
DataType data;
struct node *next;
}Queue;
Queue *Init(){
Queue *q=new Queue;
if(!q){cout<<"内存不足!\n"; return 0;}
q->next=q; return q;
}
int In(Queue *&q,DataType x){
Queue *p=new Queue;
if(!p){cout<<"内存不足!\n"; return 0;}
p->next=q->next; q=q->next=p;
p->data=x; return 1;
}
DataType *Out(Queue *q){
if(q==q->next){cout<<"队空!\n"; return 0;}
Queue *p=q->next;
q->next=p->next;
delete p; return &q->next->data;
}
void main(){
Queue *q=Init();
if(!q)return;
cout<<"1入队列\n"; if(!In(q,1))return;
cout<<"2入队列\n\t"; if(!In(q,2))return;
cout<<*Out(q)<<"出队列\n";
cout<<"3入队列\n\t"; if(!In(q,3))return;
cout<<*Out(q)<<"出队列\n\t";
cout<<*Out(q)<<"出队列\n";
}
[em3]
请将该题的回传方式改成用“引用回传”
#include<iostream.h>
typedef int DataType;
typedef struct node{
DataType data;
struct node *next;
}Queue;
Queue *Init(){
Queue *q=new Queue;
if(!q){cout<<"内存不足!\n"; return 0;}
q->next=q; return q;
}
int In(Queue *&q,DataType x){
Queue *p=new Queue;
if(!p){cout<<"内存不足!\n"; return 0;}
p->next=q->next; q=q->next=p;
p->data=x; return 1;
}
DataType *Out(Queue *q){
if(q==q->next){cout<<"队空!\n"; return 0;}
Queue *p=q->next;
q->next=p->next;
delete p; return &q->next->data;
}
void main(){
Queue *q=Init();
if(!q)return;
cout<<"1入队列\n"; if(!In(q,1))return;
cout<<"2入队列\n\t"; if(!In(q,2))return;
cout<<*Out(q)<<"出队列\n";
cout<<"3入队列\n\t"; if(!In(q,3))return;
cout<<*Out(q)<<"出队列\n\t";
cout<<*Out(q)<<"出队列\n";
}
[em3]