>>>Hey Everyone,
>>>
>>>I've been doing some exploring on the net about data structures. EVERY
>>>tutorial/explanation/what-have-you tells you what they are, how they
>>>work, etc., but they do not under any circumstances tell you what you
>>>would use them for...why would I use a stack, or a linked list, or
>>>whatever? They also don't say how to use them in {programming language}.
>>>This has been quite frustrating. Can anyone give me a hint, or point me
>>>to some documentation that talks about why you'd want to use a given data
>>>structure?
>>>
Russell,
Most data structures could be used for most tasks. You need to
learn the advantages, disadvantages, and absolute limits of each kind of
data structure in order to jusge which one should be used in any
situation. A stack can easily be implemented as an integer (to indicate
the current top of the stack) and an array (for most low-level
languages, using an array means that you are setting a fixed stack
size). Stacks can be accessed very quickly:
void *pop()
{
if (top>-1)
return(buf[top--]);
else
return(NULL);
}
A linked-list would be MUCH slower (and much more complicated code).
In a multi-threaded program, you would add a Mutex to protect the
stack from corruption. I once used a stack to implement a "MutexPool";
the program allocated a user-defined number of mutexes which a large
tree of objects would use during operations (this was an embedded RTOS
where system level resources like mutexes could not be allowed to grow
unchecked). When a thread was about to operate on an object, the thread
called the object's Lock() method, which popped an available mutex off
of the top of the MutexPool stack. When the mutex was UnLocked(), that
mutex was pushed back onto the top of the MutexPool stack. This was
fast and simple.
That same project used queues to hold, filter, and forward
networking messages (this was a publish/subscribe network messaging
system). In fact, these were priority queues, where a high-priority
message could be pushed into a queue, and during insert into the queue,
it would be sorted toward to front of the queue so that it was dequeued
before any lower priority messages. Choosing that data structure was
easy, because of the nature of the task (compared to the nature of
queues).
--ronan
This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 19:36:48 EDT