Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote