i am tryinh to create stack using linked list.
function below is to pop the last element and my problem is that the previous ptr is the same as current ptr though i did delete it after returning the string to the function.
where did i do wrong?
Code:std::string Stack::pop() { node *ptr, *ptr2 = NULL; //empty if (start == NULL ){ return ""; } //more items for (ptr = start ; ptr->next != NULL ;ptr=ptr->next){ ptr2=ptr; cout << "ptr2: " << ptr2->data << endl; cout << "ptr: " << ptr->data << endl; } std::string ans = ptr->data; return ans; delete ptr; }
thanks.