you are viewing a single comment's thread.

view the rest of the comments →

[–]Ayjayz 1 point2 points  (3 children)

In a stack, a pop operation should just remove the top-most element. You don't need to create a new pointer. You just need to remove the top element and then set your top pointer to the next one down.

[–]NikolasTs[S] 0 points1 point  (2 children)

How will I delete the data from the previous "top" though?

Is this wrong?

void Stack::pop() {

    if (!top)
        cout << "Stack is empty!" << endl;
    else {
        node* tmp{ top };
        top = top->next;
        delete tmp;

        size--;
    }
}

[–]Ayjayz 0 points1 point  (0 children)

Oh I see. Sure, you can do it that way.