A stack is a linear knowledge construction wherein components are accessed, inserted and deleted from one finish referred to as the high of the stack. Stack follows the LIFO ( Final In First Out) strategy. Two primary operations on a stack are push and pop.
A queue can also be a linear knowledge construction wherein components are inserted from one finish referred to as the rear finish and deleted from the opposite finish referred to as the entrance finish. Queue follows FIFO ( First In First Out ) strategy. Two primary operations on the queue are – enqueue and dequeue.
An array is a linear knowledge construction that mixes the info of the identical sort. It’s a assortment of comparable knowledge sorts wherein components are accessed with the assistance of indices. The weather within the arrays are saved in contiguous reminiscence areas. Array is a static knowledge construction with restricted dimension.
Why and When stack or queue is used as a substitute of arrays/lists:
- We use stack or queue as a substitute of arrays/lists once we need the weather in a selected order i.e. within the order we put them (queue) or within the reverse order (stack).
- Queues and stacks are dynamic whereas arrays are static. So once we require dynamic reminiscence we use queue or stack over arrays.
- Stacks and queues are used over arrays when sequential entry is required.
- To effectively take away any knowledge from the beginning (queue) or the tip (stack) of a knowledge construction.