char ch;
int fq;
leafN(char c, int f){
ch = c;
fq = f;
}
~leafN(){
delete this;
}
};
int main(){
priority_queue<int> pq;
cout << "Enter the number of characters\n";
int n;
cin>>n;
leafN *leaf[n];
cout << "Enter the character and its frequency \n";
for(int i=0; i<n; i++){
int a,w;
cin>>a>>w;
leaf[i] = new leafN(a,w);
}
for(int i=0; i<n; i++){
cout << leaf[i]->ch << " " << leaf[i]->fq << endl;
}
} Please help, why this doesn't work?
it's not taking in n inputs into leaf pointer array
hmm. don't use bits/stdc++ header. it's honestly annoying for everyone else who doesn't do competitive programming, not to mention it is a bad habit
Any visible error in my code or is it due to the <bits/stdc++.h> library?
Is it ok to use delete this?🤨
nope, it shouldn't be a problem. I am running it to see what actually happens, wait for me or other members to respond
You get input as int a but you use it as a char in your constructor?
Delete each by a loop Don't use delete this
silly mistake oh my god, srry fr wasting y'all time
Happy to help👍
i don't want to delete the data, I put a destructor only for formality
That destructor shouldn't do anything
I just don't see any need for anything to be done in destructor But you have to delete each variable in the array with a loop
In your main function or wherever you create a pointer
hmm.. there's a few things that are very wrong about your code: 1. What is leafN supposed to be? Are you modelling a tree structure? 2. Why do you allocate individual leafs in the for loop? Why not just allocate it outside the for loop? ah it's an array of pointers nvm :)
Yes, kind of tree. Actually it's an incomplete code, i was trying to implement huffman tree
i see, that makes much sense now
Обсуждают сегодня