主函数部分的错误不会改,求帮忙
题目如下
编写一个程序,任意输入n个100以内的数,将它们的奇数和偶数分别存入链队为Q1和Q2中,然后配对输出链队Q1、Q2中的值,直到任一队列为空为止。
#include
#include
# define OK 1
# define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int QElemType;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue &Q){
//构造新的队列
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front) exit(OVERFLOW);
Q.front->next=NULL;
return OK;
}
Status EnQueue(LinkQueue &Q, QElemType e){
//插入元素e为Q的新的队尾元素
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
p->next=NULL;
p->data=e;
Q.rear->next=p;
Q.rear=p;
return OK;
}
Status DeQueue(LinkQueue &Q,QElemType &e){
//若队不空,删除Q的队头元素,并用e返回其值
QueuePtr p;
if(Q.front==Q.rear) return ERROR;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
free(p);
return OK;
}
int main(){
struct Q1,Q2;
InitQueue(&Q1);
InitQueue(&Q2);
int cou,cou1,cou2;
scanf("%d",&cou);
int a,i,j;
for(i=0;iscanf("%d",&a[i]);
if(a[i]%2==0){
EnQueue(&Q1,a[i]);
cou1++;}
else{
EnQueue(&Q2,a[i]);
cou2++;
}
}
int min,n1,n2;
min=(cou1<=cou2)?cou1:cou2;
for(j=0;jDeQueue(&Q1,&n1);
DeQueue(&Q1,&n2);
printf("%d %d\n",n1,n2);
}
return 0;
}
参考GPT和自己的思路:
根据题目要求,需要将输入的数按照奇偶性放入两个队列中,然后进行配对输出。对于给出的代码,有以下几处问题:
Q1
和 Q2
,但是定义方式不正确,应该改为:LinkQueue Q1, Q2;
InitQueue
函数初始化队列时,应该传入队列的地址而不是队列本身,因此调用应该改为:InitQueue(&Q1);
InitQueue(&Q2);
scanf("%d", &a[i]);
改为:
scanf("%d", &a);
int cou, cou1 = 0, cou2 = 0;
在将偶数存入 Q1 中时,应该将变量 cou1++ 改为 cou2++,因为偶数应该存入 Q2 中,而不是 Q1 中。
在输出配对值时,应该将 DeQueue 函数的第一个参数改为 &Q2,因为偶数存入了 Q2 中。
修改后的代码如下:
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int QElemType;
typedef struct QNode{
QElemType data;
struct QNode *next;
} QNode, *QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
} LinkQueue;
Status InitQueue(LinkQueue *Q){
//构造新的队列
Q->front = Q->rear = (QueuePtr) malloc(sizeof(QNode));
if(!Q->front)
exit(OVERFLOW);
Q->front->next = NULL;
return OK;
}
Status EnQueue(LinkQueue *Q, QElemType e){
//插入元素e为Q的新的队尾元素
QueuePtr p;
p = (QueuePtr) malloc(sizeof(QNode));
if(!p)
exit(OVERFLOW);
p->next = NULL;
p->data = e;
Q->rear->next = p;
Q->rear = p;
return OK;
}
Status DeQueue(LinkQueue *Q, QElemType *e){
//若队不空,删除Q的队头元素,并用e返回其值
QueuePtr p;
if(Q->front==Q->rear)
return ERROR;
p = Q->front->next;
*e = p->data;
Q->front->next = p->next;
if(Q->rear==p)
Q->rear = Q->front;
free(p);
return OK;
}
int main(){
LinkQueue Q1, Q2;
InitQueue(&Q1);
InitQueue(&Q2);
int cou, cou1 = 0, cou2 = 0;
scanf("%d",&cou);
int a,i,j;
for(i = 0; i < cou; i++){
scanf("%d", &a);
if(a % 2 == 0){
EnQueue(&Q2, a);
cou2++;
} else {
EnQueue(&Q1, a);
cou1++;
}
}
int min, n1, n2;
min = (cou1 <= cou2) ? cou1 : cou2;
for(j = 0; j < min; j++){
DeQueue(&Q1, &n1);
DeQueue(&Q2, &n2);
printf("%d %d\n", n1, n2);
}
return 0;
}
参考GPT和自己的思路:
在代码中有一些问题,需要进行修改:
1.声明Q1和Q2时应该使用LinkQueue类型,而不是struct类型。
2.在输入循环中,应该将偶数插入Q2中,将奇数插入Q1中。
3.在输出循环中,应该先从Q2中取出元素,再从Q1中取出元素,以实现配对输出。
以下是修改后的正确代码:
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int QElemType;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue &Q){
//构造新的队列
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front) exit(OVERFLOW);
Q.front->next=NULL;
return OK;
}
Status EnQueue(LinkQueue &Q, QElemType e){
//插入元素e为Q的新的队尾元素
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
p->next=NULL;
p->data=e;
Q.rear->next=p;
Q.rear=p;
return OK;
}
Status DeQueue(LinkQueue &Q,QElemType &e){
//若队不空,删除Q的队头元素,并用e返回其值
QueuePtr p;
if(Q.front==Q.rear) return ERROR;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
free(p);
return OK;
}
int main(){
LinkQueue Q1,Q2;
InitQueue(Q1);
InitQueue(Q2);
int cou,cou1=0,cou2=0;
scanf("%d",&cou);
int a[i],i,j;
for(i=0;i<cou;i++){
scanf("%d",&a[i]);
if(a[i]%2==0){
EnQueue(Q2,a[i]);
cou2++;
}
else{
EnQueue(Q1,a[i]);
cou1++;
}
}
int min,n1,n2;
min=(cou1<=cou2)?cou1:cou2;
for(j=0;j<min;j++){
DeQueue(Q1,n1);
DeQueue(Q2,n2);
printf("%d %d\n",n1,n2);
}
return 0;
}
还有错误可以帮忙改一下吗
main.c:16:28: error: expected ';', ',' or ')' before '&' token
Status InitQueue(LinkQueue &Q){
^
main.c:23:26: error: expected ';', ',' or ')' before '&' token
Status EnQueue(LinkQueue &Q, QElemType e){
^
main.c:34:26: error: expected ';', ',' or ')' before '&' token
Status DeQueue(LinkQueue &Q,QElemType &e){