Does D have a Queue and Stack container?

bearophile bearophileHUGS at lycos.com
Sun Jan 13 12:16:44 PST 2013


Peter Alexander:

> Well, a stack is just an array.
>
> int[] stack;
> stack ~= 1;
> stack ~= 2;
> assert(stack.back == 2);
> stack.popBack();
> assert(stack.back == 1);
> stack.popBack();
> assert(stack.empty);
>
> If you want strict stack semantics (i.e. *only* allow access to 
> the top/back) then you could trivially write a wrapper around 
> an array that does this.

That's very slow.


> For queues, you could use DList, which is a doubly-linked list. 
> Use .front to get the front of the queue, and .insertBack(x) to 
> add to the back of the queue.

Linked list are very slow, unless you have to add and delete many 
items in the middle of the sequence.


> In C++, std::stack and std::queue are just wrappers around the 
> other standard containers.

The standard container you refer to is deque, sometimes 
implemented as a dynamic array of fixed-sized arrays, and this 
data structure is not present in Phobos.

Bye,
bearophile


More information about the Digitalmars-d mailing list