Go icon indicating copy to clipboard operation
Go copied to clipboard

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

Open BulkBeing opened this issue 2 years ago • 7 comments

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.

BulkBeing avatar Sep 29 '22 04:09 BulkBeing

Feel free to open a pull request fixing this issue.

raklaptudirm avatar Sep 29 '22 05:09 raklaptudirm

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

11-aryan avatar Sep 27 '23 06:09 11-aryan

Yes, it is open.

raklaptudirm avatar Sep 29 '23 12:09 raklaptudirm

hey assign me this issue under hacktoberfest

nancy-singh10 avatar Sep 29 '23 12:09 nancy-singh10

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.

raklaptudirm avatar Sep 29 '23 12:09 raklaptudirm

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

11-aryan avatar Oct 02 '23 06:10 11-aryan

Yes it can.

raklaptudirm avatar Oct 02 '23 07:10 raklaptudirm