Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't Stack<T> and Queue<T> have Capacity property while List<T> does?

Is Capacity property more useful in a List than in the other collections such as Stack and Queue? Or is there another way to get the capacity of a Stack or a Queue?

like image 264
Setyo N Avatar asked Jun 27 '13 08:06

Setyo N


People also ask

Why use a stack instead of a list?

We use stack or queue instead of arrays/lists when we want the elements in a specific order i.e. in the order we put them (queue) or in the reverse order (stack). Queues and stacks are dynamic while arrays are static. So when we require dynamic memory we use queue or stack over arrays.

Why is a queue better than a list?

Queue is significantly faster than List , where memory accesses are 1 vs. n for List in this use case. I have a similar use case but I have hundreds of values and I will use Queue because it is an order of magnitude faster. A note about Queue being implemented on top of List : the key word is "implemented".

What happens if you fill up all the contents of a queue can you still add more values?

Queue is completely full when rear is at last array position i.e. (MaxSize -1). Now no more elements can be inserted in queue even if the queue has some empty spaces. This is a drawback of simple queues.

What is the maximum size of queue in C#?

Whenever you add an item >=length, the capacity is doubled. As you said, this does limit it to the same limitations as an array in terms of # of elements, which is actually normally 2,146,435,071 elements (not 2^31) for everything except byte values.


2 Answers

I think that the reason that List has a Capacity property and Stack and Queue do not is that the normal usage of those types is different.

For a List it is fairly common to populate it with a large set of values, even some time after it has been created. Providing the Capacity property (and constructor argument) helps to mitigate the number of reallocations that would be done when adding a large number of items to the list.

Stack and Queue on the other hand do not tend to have large numbers of items added to them at once after they've been created.

Presumably Microsoft decided that it wasn't worth adding the Capacity property because it wouldn't be used very much.

However, do note that Queue does have a constructor that allows you to specify an initial capacity, and so does Stack.

Also note that both classes also have a TrimExcess() method, as mentioned by @drch below.

So Microsoft thought it would be useful at construction time, but not useful later on - so they only added the capacity functionality to the constructors.

(Incidentally I've just had a quick check through our code base, and it seems that the only time we use a capacity for List is in fact at construction time. So maybe if Microsoft were designing List now, they might also omit the Capacity property for List...)

like image 171
Matthew Watson Avatar answered Sep 28 '22 04:09

Matthew Watson


Stack and Queue are LIFO and FIFO structures respectively.

In both cases, you (as a consumer of the API) generally only need to know how to put data into the structure, and how to get data out again. You aren't concerned with the length of the data structure, only with push and pop.

If you need to get the capacity for any reason (a bounded stack/queue perhaps?) then it'd probably be better to hide that detail from the end user and implement your own stack/queue structure.

like image 33
Jeff Foster Avatar answered Sep 28 '22 04:09

Jeff Foster