关于C++队列queue的一个问题 编译通过但运行是出错 鄙人才疏学浅未改成功 望高手帮忙改一下 不胜感激
structnode{intdata;structnode*next;};structqueue{structnode*front;structnode*rear;};s...
struct node
{int data;
struct node *next;
};
struct queue
{struct node *front;
struct node *rear;
};
struct queue * initqueue()
{struct queue *q;
q=(struct queue *)malloc(sizeof(queue));
q->front=q->rear=(struct node *)malloc(sizeof(node));
if(q->front==NULL)
return 0;
q->front->next=NULL;
return q;
}
void creatqueue(struct queue *q,int n)
{
struct node *p;
for(int i=0;i<n;i++)
{p=(struct node *)malloc(sizeof(node));
cout<<"请输入第"<<i+1<<"个元素";
cin>>p->data;
p->next=NULL;
q->rear->next=p;
q->rear=p;
}
}
void outputqueue(struct queue *q)
{struct node *p;
p=q->front->next;
while(p!=NULL)
{cout<<p->data<<",";
p=p->next;
}
}
void deletequeue(struct queue *q)
{if(q->front==q->rear)
{cout<<"这是个空队列,无法进行删除操作";
exit(0);
}
struct node *p;
p=(struct node *)malloc(sizeof(node));
p=q->front->next;
cout<<"已删除元素:"<<p->data;
q->front->next=p->next;
if(q->rear==p)
q->rear=q->front;
free(p);
}
void enqueue(struct queue *q)
{struct node *p;
p=(struct node *)malloc(sizeof(node));
cout<<"请输入需要插入的数据:";
cin>>p->data;
q->rear->next=p;
q->rear=p;
p->next=NULL;
}
void destroyqueue(struct queue *q)
{while(q->front!=NULL)
{
q->rear=q->front->next;
if(q->front->next!=NULL)
cout<<q->rear;
free(q->front);
q->front=q->rear;
}
cout<<"已销毁队列";
}
main()
{
struct queue *q;
int n;
int flag;
do{
cout<<endl<<"************队列操作**************"<<endl;
cout<<" 1 创建 2 删除 "<<endl;
cout<<"**********************************"<<endl;
cout<<" 3 插入 4 销毁 "<<endl;
cout<<"**********************************"<<endl;
cout<<" 5 列出 6 退出 "<<endl;
cout<<"**********************************"<<endl;
cout<<"请输入你的选择:";
cin>>flag;
switch(flag)
{
case 1:{
cout<<"请输入所要创建的队列的元素个数:";
cin>>n;
creatqueue(q,n);
break;
}
case 2:{
deletequeue(q);
cout<<endl;
break;}
case 3:{
enqueue(q);
break;}
case 4:{
destroyqueue(q);
break;}
case 5:{
cout<<"\nq:";
outputqueue(q);
cout<<endl;
break;
}
case 6: exit(0);
}
}
while(1);
} 展开
{int data;
struct node *next;
};
struct queue
{struct node *front;
struct node *rear;
};
struct queue * initqueue()
{struct queue *q;
q=(struct queue *)malloc(sizeof(queue));
q->front=q->rear=(struct node *)malloc(sizeof(node));
if(q->front==NULL)
return 0;
q->front->next=NULL;
return q;
}
void creatqueue(struct queue *q,int n)
{
struct node *p;
for(int i=0;i<n;i++)
{p=(struct node *)malloc(sizeof(node));
cout<<"请输入第"<<i+1<<"个元素";
cin>>p->data;
p->next=NULL;
q->rear->next=p;
q->rear=p;
}
}
void outputqueue(struct queue *q)
{struct node *p;
p=q->front->next;
while(p!=NULL)
{cout<<p->data<<",";
p=p->next;
}
}
void deletequeue(struct queue *q)
{if(q->front==q->rear)
{cout<<"这是个空队列,无法进行删除操作";
exit(0);
}
struct node *p;
p=(struct node *)malloc(sizeof(node));
p=q->front->next;
cout<<"已删除元素:"<<p->data;
q->front->next=p->next;
if(q->rear==p)
q->rear=q->front;
free(p);
}
void enqueue(struct queue *q)
{struct node *p;
p=(struct node *)malloc(sizeof(node));
cout<<"请输入需要插入的数据:";
cin>>p->data;
q->rear->next=p;
q->rear=p;
p->next=NULL;
}
void destroyqueue(struct queue *q)
{while(q->front!=NULL)
{
q->rear=q->front->next;
if(q->front->next!=NULL)
cout<<q->rear;
free(q->front);
q->front=q->rear;
}
cout<<"已销毁队列";
}
main()
{
struct queue *q;
int n;
int flag;
do{
cout<<endl<<"************队列操作**************"<<endl;
cout<<" 1 创建 2 删除 "<<endl;
cout<<"**********************************"<<endl;
cout<<" 3 插入 4 销毁 "<<endl;
cout<<"**********************************"<<endl;
cout<<" 5 列出 6 退出 "<<endl;
cout<<"**********************************"<<endl;
cout<<"请输入你的选择:";
cin>>flag;
switch(flag)
{
case 1:{
cout<<"请输入所要创建的队列的元素个数:";
cin>>n;
creatqueue(q,n);
break;
}
case 2:{
deletequeue(q);
cout<<endl;
break;}
case 3:{
enqueue(q);
break;}
case 4:{
destroyqueue(q);
break;}
case 5:{
cout<<"\nq:";
outputqueue(q);
cout<<endl;
break;
}
case 6: exit(0);
}
}
while(1);
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询