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

Use slice appending instead of insertion at zeroth index in "stack array" implementation #543

Open
BulkBeing opened this issue Sep 29, 2022 · 7 comments
Labels
dont-close enhancement progress Solving this issue is in progress.

Comments

@BulkBeing
Copy link

Description
Current stack array implementation stores the last inserted value at zeroth index of the backing slice. This is inefficient as each push operation will require a new allocation and copying of contents in the current slice.

func stackPush(n any) {
	stackArray = append([]any{n}, stackArray...)
}

Inserting latest element to the last index (append) will also help in reusing existing slices's remaining capacity when multiple push and pop operations happens in mixed order.
Also, the current implementation uses a global variable as the backing slice. It should be implemented as an object and global mutable variables should be avoided.

@raklaptudirm
Copy link
Member

Feel free to open a pull request fixing this issue.

@github-actions github-actions bot added the stale label Oct 30, 2022
@TheAlgorithms TheAlgorithms deleted a comment from github-actions bot Oct 30, 2022
@raklaptudirm raklaptudirm added progress Solving this issue is in progress. dont-close and removed stale labels Oct 30, 2022
@11-aryan
Copy link
Contributor

Hello,
Is this issue still open and can this be included for hacktoberfest ?

@raklaptudirm
Copy link
Member

Yes, it is open.

@nancy-singh10
Copy link

hey assign me this issue under hacktoberfest

@raklaptudirm
Copy link
Member

Issues will not be assigned to a single individual. If anyone wishes to work on any issue just open a pull request and we will review it.

@11-aryan
Copy link
Contributor

11-aryan commented Oct 2, 2023

Can the code style tests that failed be resolved by running gofmt -s -w stackarray.go ?

@raklaptudirm
Copy link
Member

Yes it can.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dont-close enhancement progress Solving this issue is in progress.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants