简体   繁体   English

程序编译但未运行 c++

[英]program compiling but not running c++

Its not an homework assignment I'm coding binary tree with queue but the program is not working its compiling but not printing anything它不是家庭作业我正在用队列编码二叉树,但程序没有进行编译但没有打印任何东西

  • I tried clearing the cin buffer but still the problem persists我尝试清除 cin 缓冲区,但问题仍然存在
  • I used vscode and sublime text for running the code我使用 vscode 和 sublime text 运行代码
  • I'm using cpp14我正在使用 cpp14
  • exe file is made -other programs are working properly exe文件已制作 - 其他程序正常工作

Code for queue队列代码

#include<bits/stdc++.h>
using namespace std;

class treeNode{
    public:
    treeNode *lchild;
    int data;
    treeNode *rchild;
};

class circularQueue{

    int f;//front
    int r;//rear
    int s;//back
    treeNode **Q; 
    public:
    circularQueue(int size){
        this->f=0;
        this->r=0;
        this->s = size;
        *Q = new treeNode;
    }

    void enqueue(treeNode *val);
    treeNode* deque();
    int isEmpty();
};

void circularQueue :: enqueue(treeNode *val){

    if((r+1)%s == f){
        cout<<"Queue is full\n";
    }
    else{
        r = (r+1) % s;
        Q[r] = val;
    }

}

treeNode* circularQueue :: deque(){

    treeNode *x = NULL;

    if(f == r){
        cout<<"Queue is empty\n";
    }
    else{
        f = (f+1)%s;
        x = Q[f];
    }
    return x;
}

int circularQueue::isEmpty(){
    return f==r;
}

code for tree creation树创建代码

#include<bits/stdc++.h>
#include "queue.h"
using namespace std;

treeNode *root = NULL;
void create(){
    treeNode *p,*t;
    int x;
    circularQueue q(100);
    std::cin.ignore(INT_MAX);
    cout<<"enter root value\n";
    cin>>x;

    root->data = x;
    root->lchild=root->rchild = NULL;

    q.enqueue(root);

    while(!q.isEmpty()){
        p = q.deque();
        cout<<"enter value of lchild"<<p->data<<endl;
        cin>>x;
        if(x!=-1){
            t = new treeNode();
            t->data = x;
            t->lchild->rchild = NULL;
            p->lchild = t;
            q.enqueue(t);
        }

    }
 cout<<"enter value of rchild"<<p->data<<endl;
        cin>>x;
        if(x!=-1){
            t = new treeNode();
            t->data = x;
            t->rchild->rchild = NULL;
            p->lchild = t;
            q.enqueue(t);
        }

}

void preorder(treeNode *p){
    if(p){
        cout<<p->data<<" ";
        preorder(p->lchild);
        preorder(p->rchild);
    }

}
int main(){

    create();
    preorder(root);

}

In this line在这一行

*Q = new treeNode;

you are de-referencing an uninitialized pointer (undefined behavior).您正在取消引用未初始化的指针(未定义的行为)。 I wonder why you are using a pointer to a pointer?我想知道你为什么使用指向指针的指针? Seems unnecessary.似乎没有必要。

If the undefined behavior does not lead to a crash, your program will seemingly stop here:如果未定义的行为不会导致崩溃,您的程序似乎会停在这里:

std::cin.ignore(INT_MAX);

You are essentially waiting until STDIN ends.您实际上是在等待 STDIN 结束。

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM