簡體   English   中英

程序編譯但未運行 c++

[英]program compiling but not running c++

它不是家庭作業我正在用隊列編碼二叉樹,但程序沒有進行編譯但沒有打印任何東西

  • 我嘗試清除 cin 緩沖區,但問題仍然存在
  • 我使用 vscode 和 sublime text 運行代碼
  • 我正在使用 cpp14
  • exe文件已制作 - 其他程序正常工作

隊列代碼

#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;
}

樹創建代碼

#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);

}

在這一行

*Q = new treeNode;

您正在取消引用未初始化的指針(未定義的行為)。 我想知道你為什么使用指向指針的指針? 似乎沒有必要。

如果未定義的行為不會導致崩潰,您的程序似乎會停在這里:

std::cin.ignore(INT_MAX);

您實際上是在等待 STDIN 結束。

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM