Skip to content

why not use linked list for stack c# #65295

Answered by Clockwork-Muse
Xyncgas asked this question in General
Discussion options

You must be logged in to vote

Trivial (possibly not the reason) answer - it takes less memory. For classes, a linked list takes at least twice the memory of an array (An array will contain one pointer to the data on the heap, but a linked list will need one pointer to the data, plus a pointer to the next node).

Things are slightly better if the contents are structs, because the contents of the struct also occupy container memory (and so don't have pointer to the data), but it still takes some extra memory.

Too, while it's true that a linked list makes it cheaper to insert/remove elements from the middle of the list, in the case of a stack this isn't necessarily helpful, because most modifications are going to happen a…

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@Xyncgas
Comment options

@danmoseley
Comment options

@John0King
Comment options

@Joe4evr
Comment options

@John0King
Comment options

Answer selected by danmoseley
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
5 participants