Re: You don't have to move or free pointers, fragmenting your heap.
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Thu, 27 Sep 2007 10:56:22 -0500
"Jeff?Relf" <Jeff_Relf@xxxxxxxxx> wrote in message
news:Jeff_Relf_2007_Sep_27__1_30_AH@xxxxxxxxxxxx
Has anyone ever implemented a stack using a linked list ?
Who, exactly, thinks linked-lists are faster and/or better ?
I've spent years coding ( for money ) in FORTH and PostScript,
so I'm quite familiar with indexing into the array that is the stack.
In VC++ 8.0, every local variable is
an index into " the array that is the stack ",
not a journey down a linked list, not a thread.
Only one end of a stack is dynamic.
You don't have to move or free pointers, fragmenting your heap;
instead, you can create dedicated heaps and destroy them.
With a dynamic array of pointers ( instead of a linked list )
you can do things like: copy-off a chunck of pointers,
quick-sort it any way you like and do binary searches on it.
Try doing that with linked lists !
With a linked list, you can move an arbitrary length sequence elements into
a different list (splice operation) in fixed time. Try that with your
dynamic array!
As for why you might implement a stack with a linked list, what if you had a
fixed number (or fixed maximum) of objects that would be stored on different
stacks (this could be LIFO assembly line processing)? If you use dynamic
arrays, every array needs to be large enough to accommodate the maximum
possible length. With linked lists, you need only one pool of objects and
attach and detach them from the stacks, with an extra stack for the free
pool. Zero allocation and deallocation. Much less memory used.
.
- Follow-Ups:
- Two stacks means two heaps, two dynamic arrays of pointers.
- From: Jeff☠Relf
- Two stacks means two heaps, two dynamic arrays of pointers.
- References:
- I suggest binary searches not “ linklist.h ”.
- From: Jeff☠Relf
- Re: I suggest binary searches not " linklist.h ".
- From: Ben Voigt [C++ MVP]
- How does a linked-list optimize a stack ?
- From: Jeff☠Relf
- Re: How does a linked-list optimize a stack ?
- From: Tim Roberts
- Linked-lists don't make sense.
- From: Jeff☠Relf
- Re: Linked-lists don't make sense.
- From: Tim Roberts
- You don't have to move or free pointers, fragmenting your heap.
- From: Jeff☠Relf
- I suggest binary searches not “ linklist.h ”.
- Prev by Date: Re: Tom can do what ever he wants during those 5 milliseconds.
- Next by Date: Re: VC++ 6.0 To .NET
- Previous by thread: You don't have to move or free pointers, fragmenting your heap.
- Next by thread: Two stacks means two heaps, two dynamic arrays of pointers.
- Index(es):
Relevant Pages
|