structヒープのデストラクタに問題があります。 1つを追加するだけで、それを使用しない場合は、実行時例外(メモリアクセス)が作成されます。私はそれをやろうとする二日目であり、明日は締め切りです。デストラクタ定義を追加すると実行中例外が発生する
struct Heap
{
int n;
int* tab;
int* numerWKopcu;
Heap() { n=0; }
Heap (int size) { this->tab = new int[liczbaDomow]; n=0; this->numerWKopcu = new int[2000100];}
int max() { return tab[1]; }
bool empty() { return n==0; }
bool insert(int x)
{
n++;
tab[n]=x;
this->numerWKopcu[x] = n;//ZMIANA
upHeap(n);
return true;
}
bool delMin()
{
if (n<1) return false;
this->numerWKopcu[tab[n]] = 1; //ZMIANA
tab[1]=tab[n]; n--;
downHeap(1);
return true;
}
void upHeap(int x){
int p;
int mem = tab[x];
while (x>1)
{
p=x/2;
if (color[mem]>color[tab[p]]) break;
this->numerWKopcu[tab[p]] = x; //ZMIANA
tab[x]=tab[p];
x=p;
}
this->numerWKopcu[mem] = x;//ZMIANA
tab[x]=mem;
}
void downHeap (int x)
{
int s=2*x;
int mem=tab[x];
while(s<=n)
{
if (s+1<=n && color[tab[s]]>color[tab[s+1]])
s++;
if (color[mem]>color[tab[s]])
{
this->numerWKopcu[tab[s]] = x; //ZMIANA
tab[x]=tab[s];
x=s;
s=2*x;
}
else break;
}
this->numerWKopcu[mem] = x;//ZMIANA
tab[x]=mem;
}
void write()
{
for (int i=1;i<=n;i++) printf ("%d) %d\n", i, tab[i]);
printf ("\n");
}
void build()
{
int s = n;
for (s=n/2; s>=1; s--) downHeap(s);
}
/~Heap() {
delete []this->numerWKopcu;
delete []this-> tab;
};
};
正確なエラーメッセージを投稿してください... – Nick
このコードは不完全です(例えば、 'liczbaDomow'は未定義です)、英語以外の変数名を使用しています。どちらも人々があなたを助けることを非常に難しくします – thiton
デバッガで実行しようとしましたか? –