Open In App

Why and when to use Stack or Queue instead of Arrays/Lists?

Stack:

A stack is a linear data structure in which elements are accessed, inserted and deleted from one end called the top of the stack. Stack follows the LIFO ( Last In First Out) approach. Two basic operations on a stack are push and pop.



Stack

Queue:

A queue is also a linear data structure in which elements are inserted from one end called the rear end and deleted from the other end called the front end. Queue follows FIFO ( First In First Out ) approach. Two basic operations on the queue are – enqueue and dequeue. 



Queue

Array:

An array is a linear data structure that combines the data of the same type. It is a collection of similar data types in which elements are accessed with the help of indices. The elements in the arrays are stored in contiguous memory locations. Array is a static data structure with limited size.

Array

Why and When stack or queue is used instead of arrays/lists:

Because they assist you in managing your data in a more specific manner than arrays and lists. It means you won’t have to wonder if someone placed an element in the midst of your list at random, messing up certain invariants when troubleshooting an issue.Random access is the nature of arrays and lists. They’re incredibly adaptable, but they’re also easily corruptible. If you wish to keep track of your data,It’s recommended to use those, previously implemented, collections when storing data as FIFO or LIFO.

Article Tags :