A Way For Learning

Advantages and Disadvantages of stacks and Queues

4 comments
Stack/Queue are ideal for enforcing sequential rules of access (LIFO & FIFO as you stated) while Array is ideal for allowing random access as desired.


 Some real life examples : 
Stack : Washing of plates - the one at the top gets washed first.
Queue : Booking movie tickets (offline mode :P )
Array : Visit to a grocery store where the customer picks what he wants.

Stacks are last in first out so two major applications that Stacks accomplish are things like State and reversing. What I mean by state is a particular setting or structure that a program has at a point in time. Say you are writing a notepad application. A good use for a stack would be the undo/redo feature. Every time the user enters something, save the state (in this case, the text) on a stack and if you need to reverse something, just pop it off the top of the undo stack and push it onto the redo stack. 

Stacks are also used for reversing things. If you push something, say a String onto a Stack one character at a time, and then construct a String from the members popped off the Stack, then the String is reversed.

Queues are good for things like storing the order in which something happened because you know that the first even happened first in the queue and the last happened last (unless it is a modified queue). 

Access order is a simple conceptual difference between array and stack/queue.  Thats surely a benefit of these data structures over array if you want constrained access. 

Along with that memory allocation place is also an important factor. 
Efficient memory usage of your program greatly depend on what data structure you choose in you design. 
Array is always allocated on process stack. So if you need an array of size lets say 1kib, that much memory of you process stack is consumed.  If you are working in kernel space (on Linux kernel mode stack is limited to 8 kib max), its always better to minimize stack size. So it is advised to use stack/queue or other data structures which can use heap memory with best efficiency.


Talking about user space where you can have good lot of memory for process stack, it is occupied until process terminates. Where as if you use stack/queue or any other data structure,  you can free that memory as soon as you are done with it.  This improves memory utilization.

4 comments :