00:00

Difference between Stack and Queue

Hey, In this article you are going to learn difference between Stack and Queue in terms of their structure, memeory uses, performance, implementation and more.

What is a Stack Data Structure ?

A stack is a linear (ordered) data structure used to store and manipulate a collection of data elements. Stack follows the LIFO (Last In First Out) principle, which means the last element added to the stack will be removed first from the stack.

In a stack, insertion and deletion of an element can be done from one end known as the top of the stack.

The stack data structure is commonly used in programming for tasks such as managing function calls, undo/redo functionality in applications, parsing expressions, backtracking algorithms, maintaining browser history, and many more.

What is a Queue Data Structure?

A Queue is a linear data structure to store and manipulate the data elements. The queue data structure apply the concept of "First in, First out" (FIFO), which means the first added element into the queue to be the first removed from the queue. Queues do not allow random insertion and deletion operations.

A Queue is open at both ends, which enables insert operations to be performed at one end called REAR or TAIL and delete operations to be performed at another end called FRONT or HEAD.

Queue Data Structure

Difference between Stack and Queue Data Structure

CharacteristicStackQueue
Princal FlowFollows LIFO (Last In First Out) principle, which means the last element added to the stack will be removed first from the stack.Follow FIFO (First in, First out) principle, which means the first added element into the queue to be the first removed from the queue.
Basic Operationspush(), pop(), peek()enqueue(), dequeue(), peek(), rear()
Complexity (Amortized)Push: O(1), Pop: O(1), Peek: O(1)Enqueue: O(1), Dequeue: O(1), Front: O(1), Rear: O(1)
Addition of ElementsElements are added to the top of the stack.Elements are added to the rear (end) of the queue.
Removal of ElementsThe last element added is the first one to be removed.The first element added is the first one to be removed.
Memory ManagementManaged with a single pointer (top)Managed with two pointers (front and rear)
OverflowStack overflow occurs if the stack exceeds its capacity.Queue overflow occurs if the queue exceeds its capacity.
Full ConditionWhen top== max-1, it means that the stack is full.When rear==max-1, it means that the queue is full.
ImplementationStack can be implemented using arrays or linked lists.Queue can be implemented using arrays, linked lists, or circular buffers.