C++中 [Error] cannot convert 'QUEUE1' to 'int' for argument '2' to 'bool enQueue(SqQueue*&, int)'是什么意思
时间: 2024-01-27 17:03:23 浏览: 30
这个错误是因为你在调用 `enQueue` 函数时,第二个参数传入的是 `QUEUE1`,而该函数的第二个参数是一个 `int` 类型,两者类型不匹配,导致编译器报错。
可能的原因是你声明了一个叫 `QUEUE1` 的变量,但是你想要传入该变量的值给 `enQueue` 函数,而不是传入变量本身。你需要将 `QUEUE1` 的值赋给一个 `int` 变量,然后将该变量作为参数传入 `enQueue` 函数中。
相关问题
请找出下列代码的问题并解决:#include<iostream> using namespace std; #define Maxsize 100 typedef struct node//二叉树结构 { char data; struct node* lchild; struct node* rchild; }BTnode; void CreateNode(BTnode*& bt) { char h; h = getchar(); if (h != '#') { bt = (BTnode*)malloc(sizeof(BTnode)); bt->data = h; CreateNode(bt->lchild); CreateNode(bt->rchild); } else bt = NULL; } void DestoryNode(BTnode*& bt) { if (bt != NULL) { DestoryNode(bt->lchild); DestoryNode(bt->rchild); free(bt); } } typedef struct//顺序队列 { BTnode* data[Maxsize]; int front; int rear; }SqQueue; typedef struct//顺序栈 { BTnode* data[Maxsize]; int top; }SqStack; void InitQueue(SqQueue*& q) { q = (SqQueue*)malloc(sizeof(SqQueue)); q->front = q->rear = -1; } void InitStack(SqStack*& s) { s = (SqStack*)malloc(sizeof(SqStack)); s->top = -1; } void DestoryQueue(SqQueue*& q) { free(q); } void DestoryStack(SqStack*& s) { free(s); } bool QueueEmpty(SqQueue* q) { return(q->front == q->rear); } bool StackEmpty(SqStack* s) { return(s->top == -1); } bool enQueue(SqQueue*& q, BTnode*& node) { if (q->rear == Maxsize - 1) return 0; q->rear++; q->data[q->rear] = node; return 1; } bool Push(SqStack*& s, BTnode*& node) { if (s->top == Maxsize - 1) return 0; s->top++; s->data[s->top] = node; return 1; } bool deQueue(SqQueue*& q, BTnode*& node) { if (q->front == q->rear) return 0; q->front++; node = q->data[q->front]; return 1; } bool Pop(SqStack*& s) { if (s->top == -1) return 0; cout << s->data[s->top] << "\t"; s->top--; return 1; } void LevelOrder(BTnode* bt) { BTnode* p; p = new BTnode; SqQueue* qu; SqStack* st; InitStack(st); InitQueue(qu); enQueue(qu, bt); while (!QueueEmpty(qu)) { deQueue(qu, p); Push(st, bt); if (p->lchild != NULL) enQueue(qu, p->lchild); if (p->rchild != NULL) enQueue(qu, p->rchild); } cout << "二叉树的自下而上,从右到左的层次遍历结果:" << endl; if (!StackEmpty(st)) Pop(st); DestoryQueue(qu); DestoryNode(p); DestoryStack(st); } int main() { BTnode* B; cout << "输入二叉树:" << endl; CreateNode(B); LevelOrder(B); DestoryNode(B); return 0; }
代码存在以下问题:
1. 在LevelOrder函数中,创建了一个新的BTnode指针p,但没有初始化,可能会引起未知的错误。
2. 在LevelOrder函数中,将二叉树节点指针p入栈时,使用了错误的变量bt,应该使用p。
3. 在LevelOrder函数中,调用Pop函数时,打印的是栈顶元素的data值,但实际上应该打印的是指针p指向的节点的data值。
修改后的代码如下:
```
#include<iostream>
using namespace std;
#define Maxsize 100
typedef struct node//二叉树结构
{
char data;
struct node* lchild;
struct node* rchild;
}BTnode;
void CreateNode(BTnode*& bt)
{
char h;
h = getchar();
if (h != '#')
{
bt = (BTnode*)malloc(sizeof(BTnode));
bt->data = h;
CreateNode(bt->lchild);
CreateNode(bt->rchild);
}
else bt = NULL;
}
void DestoryNode(BTnode*& bt)
{
if (bt != NULL)
{
DestoryNode(bt->lchild);
DestoryNode(bt->rchild);
free(bt);
}
}
typedef struct//顺序队列
{
BTnode* data[Maxsize];
int front;
int rear;
}SqQueue;
typedef struct//顺序栈
{
BTnode* data[Maxsize];
int top;
}SqStack;
void InitQueue(SqQueue*& q)
{
q = (SqQueue*)malloc(sizeof(SqQueue));
q->front = q->rear = -1;
}
void InitStack(SqStack*& s)
{
s = (SqStack*)malloc(sizeof(SqStack));
s->top = -1;
}
void DestoryQueue(SqQueue*& q)
{
free(q);
}
void DestoryStack(SqStack*& s)
{
free(s);
}
bool QueueEmpty(SqQueue* q)
{
return(q->front == q->rear);
}
bool StackEmpty(SqStack* s)
{
return(s->top == -1);
}
bool enQueue(SqQueue*& q, BTnode*& node)
{
if (q->rear == Maxsize - 1)
return 0;
q->rear++;
q->data[q->rear] = node;
return 1;
}
bool Push(SqStack*& s, BTnode*& node)
{
if (s->top == Maxsize - 1)
return 0;
s->top++;
s->data[s->top] = node;
return 1;
}
bool deQueue(SqQueue*& q, BTnode*& node)
{
if (q->front == q->rear)
return 0;
q->front++;
node = q->data[q->front];
return 1;
}
bool Pop(SqStack*& s)
{
if (s->top == -1)
return 0;
cout << s->data[s->top]->data << "\t";
s->top--;
return 1;
}
void LevelOrder(BTnode* bt)
{
BTnode* p = NULL;
SqQueue* qu;
SqStack* st;
InitStack(st);
InitQueue(qu);
enQueue(qu, bt);
while (!QueueEmpty(qu))
{
deQueue(qu, p);
Push(st, p);
if (p->lchild != NULL)
enQueue(qu, p->lchild);
if (p->rchild != NULL)
enQueue(qu, p->rchild);
}
cout << "二叉树的自下而上,从右到左的层次遍历结果:" << endl;
while (Pop(st));
DestoryQueue(qu);
DestoryStack(st);
}
int main()
{
BTnode* B;
cout << "输入二叉树:" << endl;
CreateNode(B);
LevelOrder(B);
DestoryNode(B);
return 0;
}
```
int StackEmpty(SqStack *s) { return(s->top==-1); } int StackFull(SqStack *s) { return(s->top==N-1); } int Push(SqStack *&s,int e1,int e2)//进栈 { if(s->top==N-1) return 0; s->top++; s->CarNo[s->top]=e1; s->CarTime[s->top]=e2; return 1; } int Pop(SqStack *&s,int &e1,int &e2)//出栈 { if(s->top==-1) return 0; e1=s->CarNo[s->top];//*栈顶元素赋给s* e2=s->CarTime[s->top]; s->top--;//*修改栈顶指针 return 1; } int QueueEmpty(SqQueue *q)//判断队是否为空 { return(q->front==q->rear); } int QueueFull(SqQueue *q) /*判断队满*/ { return((q->rear+1)%M==q->front); } int enQueue(SqQueue *&q,int e) /*进队*/ { if((q->rear+1)%M==q->front) return 0; q->rear=(q->rear+1)%M; q->CarNo[q->rear]=e; return 1; } int deQueue(SqQueue *&q,int &e) /*出队*/ { if(q->front==q->rear) return 0; q->front=(q->front+1)%M; e=q->CarNo[q->front]; return 1; }解释一下每段代码
1. 栈的判空和判满函数:
```
int StackEmpty(SqStack *s) {
return(s->top==-1);
}
int StackFull(SqStack *s) {
return(s->top==N-1);
}
```
这两个函数分别判断栈是否为空和是否已满,其中top表示栈顶位置,N表示栈的最大容量。
2. 栈的进栈和出栈函数:
```
int Push(SqStack *&s,int e1,int e2) {
if(s->top==N-1)
return 0;
s->top++;
s->CarNo[s->top]=e1;
s->CarTime[s->top]=e2;
return 1;
}
int Pop(SqStack *&s,int &e1,int &e2) {
if(s->top==-1)
return 0;
e1=s->CarNo[s->top];
e2=s->CarTime[s->top];
s->top--;
return 1;
}
```
Push函数实现了进栈操作,将车牌号和时间分别存储在CarNo和CarTime数组中,同时修改栈顶指针top。Pop函数实现了出栈操作,将栈顶元素的车牌号和时间赋值给e1和e2,并修改栈顶指针top。
3. 队列的判空和判满函数:
```
int QueueEmpty(SqQueue *q) {
return(q->front==q->rear);
}
int QueueFull(SqQueue *q) {
return((q->rear+1)%M==q->front);
}
```
这两个函数分别判断队列是否为空和是否已满,其中front和rear分别表示队头和队尾位置,M表示队列的最大容量。
4. 队列的进队和出队函数:
```
int enQueue(SqQueue *&q,int e) {
if((q->rear+1)%M==q->front)
return 0;
q->rear=(q->rear+1)%M;
q->CarNo[q->rear]=e;
return 1;
}
int deQueue(SqQueue *&q,int &e) {
if(q->front==q->rear)
return 0;
q->front=(q->front+1)%M;
e=q->CarNo[q->front];
return 1;
}
```
enQueue函数实现了进队操作,将车牌号存储在CarNo数组中,并修改队尾指针rear。deQueue函数实现了出队操作,将队头元素的车牌号赋值给e,并修改队头指针front。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)