-
-
Couldn't load subscription status.
- Fork 137
Update expr.c #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update expr.c #91
Conversation
Filling the stack should start from element with index 0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! The change from pre-increment/decrement to post-increment/decrement looks correct and fixes the off‐by‐one behavior:
- In
push,stack[stack_pointer++] = element;is the right choice ifstack_pointertracks the next free slot. - In
pop,return stack[--stack_pointer];correctly moves back to the last pushed element before reading.
A couple of suggestions to strengthen this and future contributions:
- Add brief comments documenting the stack invariant: "
stack_pointerpoints to the next free slot." - Include small tests (or a demo
main) for: push/pop single item, push until full, pop until empty, and verify strict LIFO. - Consider returning a status from
push/pop(e.g., int/bool with out‐param) instead of printing errors; returning0frompopis ambiguous if0.0is a valid value. - Use
size_tforstack_pointerand comparisons withSTACK_SIZE. - Optionally guard against overflow/underflow with asserts in debug builds.
Overall, nice, focused fix with clear correctness improvement. If you’re up for it, similar sanity checks and invariants across other stack/queue exercises would be super valuable.
Thanks for the PR! The change from pre-increment/decrement to post-increment/decrement looks correct and fixes the off‐by‐one behavior:
* In `push`, `stack[stack_pointer++] = element;` is the right choice if `stack_pointer` tracks the next free slot. * In `pop`, `return stack[--stack_pointer];` correctly moves back to the last pushed element before reading.A couple of suggestions to strengthen this and future contributions:
* Add brief comments documenting the stack invariant: "`stack_pointer` points to the next free slot." * Include small tests (or a demo `main`) for: push/pop single item, push until full, pop until empty, and verify strict LIFO. * Consider returning a status from `push`/`pop` (e.g., int/bool with out‐param) instead of printing errors; returning `0` from `pop` is ambiguous if `0.0` is a valid value. * Use `size_t` for `stack_pointer` and comparisons with `STACK_SIZE`. * Optionally guard against overflow/underflow with asserts in debug builds.Overall, nice, focused fix with clear correctness improvement. If you’re up for it, similar sanity checks and invariants across other stack/queue exercises would be super valuable.
Thank you for the suggestions, I would be happy to contribute more (if my time allows)!
Filling the stack should start from element with index 0.