Skip to content
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

is the stack implementation correct? #6

Closed
nandinicbit1981 opened this issue Dec 20, 2017 · 1 comment
Closed

is the stack implementation correct? #6

nandinicbit1981 opened this issue Dec 20, 2017 · 1 comment

Comments

@nandinicbit1981
Copy link

Hello,
Firstly, great work on documentation, its just perfect!
I am reading through the datastructure examples and i dont quite understand the stack implementation.
I do understand you are using an array to implement stack. But everytime we do a pop() operation, shouldn't the array element be actually removed and array size altered? Otherwise, even after doing a pop() operation the memory will still be allocated for the popped element.

Some explanation would be great! thanks!

@orrsella
Copy link
Owner

You are correct that the memory is still allocated for the popped element. This is by design, as the Stack get's an initial capacity size, and the array will forever be of that size. The array is never resized, and if we try to put more elements than capacity we'll get a RuntimeException("Stack is full").

If you want to reclaim memory, it's best to implement a stack using a linked list, allowing to easily add/remove memory.

@orrsella orrsella closed this as completed Jan 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants