Last Updated on April 26, 2023 by Abhishek Sharma
Array:
An Array is a collection of elements which are stored in a contiguous memory. The idea is simple i.e. to store the multiple elements together. Due to which we can easily calculate the position of each element by adding the memory location of the first element i.e. name of the array.
Queue:
A Queue is a linear data structure. Queue follows the FIFO rule i.e. First in First out. In other words we can say the element that goes in first is the element that comes out first. For Example A ticket Queue outside a cinema hall where the person enters the queue first will get the ticket first.
Basic Operations of Queue:
Enqueue: This operation is used to Insert an element at the end of the queue.
Dequeue: This operation is used to remove and return an element from the front of the queue.
isEmpty(): This operation is used to check whether the queue is empty or not.
isFull(): This operation is used to check whether the queue is full or not.
Peek(): This operation is used to get the value of the element from the front of the queue.
Stack:
Stack is a linear data structure in which a user can insert and delete an element from the same end which is known as a top. Stack data structure follows LIFO property i.e. Last in first out. In LIFO, the element which was inserted last will be the element which was removed first. In stack, the process of insertion is called push operation and the process of deletion of the element from the stack is known as pop. And, we can easily keep track of the last element using a pointer called top.
Difference between Array, Stack and Queue
Array | Queue | Stack |
---|---|---|
In Array, elements are accessed by their indexes. | Queue follows FIFO principle(first in first out) i.e. the element which was inserted first is the element which was deleted at the first. | Stack follows LIFO principle(Last In First Out) i.e. the element which was inserted first is the element which was deleted at the last. |
Insertion and deletion can be done from any index in the array | Insertion and deletion can be done from rear and front end. | Insertion and deletion can be done from a single end. |
Array has a fixed size. | Queue has both dynamic or fixed size. | Stack has both dynamic and fixed size. |
In Array we can store elements of the same data type. | In Queue we can store elements of different data types. | In stack we can store elements of different data types. |
Array is of two types: 1D Array and 2D array | Queues have different types: Priority queue, circular queue, double ended queue. | Stack is only of 1 type. |
This article tried to discuss the Difference between array, queue and stack. Hope this blog helps you understand the concept. To practice more problems feel free to check MYCODE | Competitive Programming at Prepbytes.