Difference Between Stack and Queue

In this article you will learn about difference between stack and queue.

Stack

A Stack data structure works on Last In First Out principle, also abbreviated as LIFO. A Stack requires only one reference variable, known as a top pointer. Stack data structures are normally represented as a vertical representation of data items.

A Stack Data Structure can be represented in static arrays as well as linked lists. In a stack, the data items can be inserted and deleted from the same end. The element to be inserted first is the last element to get deleted. Both the operations, insertion and deletion occur at the TOP reference pointer. A Stack doesn’t necessarily contain ordered collection of data items.

Stack Implementations:

Stack data structure is used in infix to postfix conversion, scheduling algorithms, evaluation of an expression and depth first search.

The condition to check if a stack is empty:

 

The condition to check if a stack is full:

 

Queue

A Queue data structure works on First In First Out principle, also abbreviated as FIFO. A Queue requires two reference variables. Queue is normally represented as a horizontal representation of data items. The Queue data structure contains FRONT and REAR as its reference points for data processing.

A Queue can also be represented in by static arrays as well as linked lists. In a Queue, the data items can be inserted and deleted from different ends. The element to be inserted first is the first element to get deleted. The insertion operation is done at rear end whereas the deletion occurs at the front end. A Queue contains ordered collection of data items. A Queue can be divided into sub sections and it has the following extensions: Double Ended Queue, Simple Queue, Priority Queue and Circle Queue.

Queue Implementations:

A Queue offers services in operations research, transportation and computer science that involves persons, data, events and objects to be stored for later processing.

The condition to check if a queue is empty:

 

The condition to check if a queue is full:

Difference Between Stack and Queue

Image Source

Difference Between Stack and Queue

Stack Queue
A Stack Data Structure works on Last In First Out (LIFO) principle. A Queue Data Structure works on First In First Out (FIFO) principle.
A Stack requires only one reference pointer. A Queue requires two reference pointers.
A Stack is primarily a vertical representation of data items. A Queue is a horizontal representation of data items.
A Stack contains TOP as its reference for data processing. A Queue contains REAR and FRONT as its reference for data processing.
The data items are inserted and deleted from the same end. The data items in a queue are inserted and deleted from different ends.
The element to be inserted first is removed last. The element to be inserted first is removed first.
To check if a stack is empty, following condition is used:

TOP == -1

To check if a queue is empty, following condition is used:

FRONT == -1 || FRONT == REAR + 1

The insertion and deletion operations occur at the TOP end of a Stack. The insertion operation occurs at REAR end and the deletion operation occurs at FRONT end.
A Stack data structure is not necessarily an ordered collection of data items. A Queue data structure is an ordered collection of data.
To check if a stack is full, following condition is used:

TOP == MAX – 1

To check if a queue is full, following condition is used:

REAR == MAX – 1

A Stack cannot be divided into sub sections and it doesn’t have extensions. A Queue can be divided into sub sections and it has the following extensions: Double Ended Queue, Simple Queue, Priority Queue and Circle Queue.
Used in infix to postfix conversion, scheduling algorithms, depth first search and evaluation of an expression. A Queue offers services in operations research, transportation and computer science that involves persons, data, events and objects to be stored for later processing.

 

Comment below if you found anything incorrect or have doubts related to above difference between stack and queue tutorial.

Category: DSA

Leave a Reply

Your email address will not be published. Required fields are marked *