Does D have a Queue and Stack container?

Peter Alexander peter.alexander.au at gmail.com
Sun Jan 13 12:02:38 PST 2013


On Sunday, 13 January 2013 at 19:55:28 UTC, Damian wrote:
> As per title, it would be awesome if someone could link me to 
> these. I'm quite suprised that D does not already contain 
> these.. are there any plans for them joining Phobos?

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.

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.

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


More information about the Digitalmars-d mailing list