Please help find the issue with my pop function below. struct NodeChunk{ string*
ID: 3623245 • Letter: P
Question
Please help find the issue with my pop function below.
struct NodeChunk{
string* val;
NodeChunk* next;
};
struct Stack{
int chunkSize;
int topElt;
NodeChunk* firstChunk;
};
bool isEmpty (Stack s) {
return s.firstChunk==NULL;
}
void pop (Stack& s) {
assert(!isEmpty(s));
if (s.topElt>0){
s.topElt--;
}
else{
if (s.firstChunk->next==NULL){
delete[] s.firstChunk->val;
s.firstChunk=NULL;
s.topElt=-1;
}
else {
NodeChunk* temp=s.firstChunk;
s.firstChunk=s.firstChunk->next;
temp->next=NULL;
delete[] temp->val;
delete temp;
s.topElt=s.chunkSize-1;
}
}
}
Explanation / Answer
struct NodeChunk{ string* val; NodeChunk* next; }; struct Stack{ int chunkSize; int topElt; NodeChunk* firstChunk; }; bool isEmpty (Stack s) { return s.firstChunk==NULL; } void pop (Stack& s) { assert(!isEmpty(s)); if (s.topElt>0){ s.topElt--; //what is this? // } // else{ if (s.firstChunk->next==NULL){ delete[] s.firstChunk->val; s.firstChunk=NULL; // s.topElt=-1; } else { NodeChunk* temp=s.firstChunk; s.firstChunk=s.firstChunk->next; temp->next=NULL; delete[] temp->val; delete temp; // s.topElt=s.chunkSize-1; } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.