implementing stacks using dynamic arrays
Sean Kelly
sean at f4.ca
Sun Apr 9 15:06:40 PDT 2006
John Demme wrote:
>
> You can use array slicing to do it:
> int[] a;
> int t;
> ...
> a ~= t;
> ...
> t = a[$-1]; //Get the last element
> a = a[0..$-1]; //Slice off the last element
>
> But Mike Capp is right: in terms of efficiency, this is a horrible way to do
> it. For a stack, you're better off with a linked list.
Interestingly, a dynamic array that doubles capacity on allocations is
typically better than a linked-list implementation these days. Linked
lists tend to have poor locality, which can result in cache problems and
page faults.
Sean
More information about the Digitalmars-d
mailing list