Stack in Data Structure - Introduction, LIFO Principle and Operations

Introduction to Stack

Stack is one of the most widely used linear data structures in computer science. It stores data in a specific order and follows a unique rule called Last In First Out (LIFO). According to this rule, the element inserted most recently is removed first.

Stacks are used in operating systems, compilers, memory management, web browsers, calculators, programming languages, and many real-world software applications. Understanding stacks helps students build a strong foundation for advanced data structures and algorithms.

Unlike arrays where elements can be accessed directly using indexes, a stack allows insertion and deletion from only one end known as the TOP. This restriction makes stack operations simple and efficient.


What is a Stack?

A Stack is a linear data structure in which insertion and deletion operations are performed from the same end. The insertion operation is called Push and the deletion operation is called Pop.

The stack follows the Last In First Out principle, meaning the newest element enters and leaves first.

Definition of Stack

A Stack is a linear data structure that follows the Last In First Out (LIFO) principle where insertion and deletion are performed only from the top position.


Real-Life Examples of Stack

Understanding stacks becomes easier when we relate them to everyday situations.

Stack of Plates

In a restaurant, plates are placed one on top of another. The last plate placed on the stack is the first plate removed.

Books on a Table

If books are stacked vertically, the book placed last will be removed first.

Browser History

Modern web browsers use stacks to store visited pages. Pressing the back button takes users to the most recently visited page.


Need for Stack Data Structure

Many computing tasks require temporary storage where recently stored information must be processed before older information. Stacks provide an efficient mechanism for handling such requirements.

Why Do We Need Stacks?


Characteristics of Stack


Understanding the LIFO Principle

LIFO stands for Last In First Out. It means the last element inserted into the stack is the first element removed.

Example

Push(10)
Push(20)
Push(30)
Push(40)

Stack Representation:

TOP
 ↓
40
30
20
10

If a Pop operation is performed, the value 40 will be removed first because it was inserted last.


Basic Terminology of Stack

Term Description
Stack A linear data structure following LIFO.
TOP Pointer to the topmost element.
Push Operation used to insert data.
Pop Operation used to remove data.
Peek Displays the top element.
Overflow Occurs when stack becomes full.
Underflow Occurs when stack becomes empty.

Stack Representation

Stacks are usually represented vertically because all operations occur from the top.

TOP
 ↓
50
40
30
20
10

The topmost element is always the first candidate for removal.


Operations on Stack

Several operations can be performed on a stack for managing stored elements.


Push Operation

Push is the operation used to insert a new element into the stack.

Every new element is added at the top position.

Example


Before Push
TOP
 ↓
30
20
10
Push(40)

After Push:

TOP
 ↓
40
30
20
10

Pop Operation

Pop removes the topmost element from the stack.

Example

Before Pop
TOP
 ↓
40
30
20
10

After Pop:

TOP
 ↓
30
20
10

Removed Element = 40


Peek Operation

Peek displays the top element without removing it from the stack.

Stack
TOP
 ↓
40
30
20
10
Peek()
Output = 40

Stack Overflow

Stack Overflow occurs when an insertion operation is attempted on a full stack.

Maximum Size = 5
Current Elements = 5
Push(60)
Result:
Stack Overflow

Stack Underflow

Stack Underflow occurs when a deletion operation is attempted on an empty stack.

Stack Empty
Pop()
Result:
Stack Underflow

Advantages of Stack


Disadvantages of Stack


Difference Between Stack and Array

Stack Array
Follows LIFO. Does not follow LIFO.
Restricted access. Direct index access.
Insertion at top. Insertion anywhere.
Special-purpose structure. General-purpose structure.

Implementation of Stack Using Array

One of the most common methods of implementing a stack is by using an array. In this approach, elements are stored in contiguous memory locations, and a variable called TOP keeps track of the last inserted element.

Array implementation is simple and suitable when the maximum stack size is known in advance.

Array Representation

#define MAX 100
int stack[MAX];
int top = -1;

Initially, the TOP value is set to -1, indicating that the stack is empty.


Push Operation Using Array

The push operation inserts a new element at the top of the stack.

void push(int value)
{
    if(top == MAX-1)
    {
        printf("Stack Overflow");
    }
    else
    {
        top++;
        stack[top] = value;
    }
}

Before inserting a new element, the program checks whether the stack is already full.


Pop Operation Using Array

The pop operation removes the topmost element from the stack.

int pop()
{
    if(top == -1)
    {
        printf("Stack Underflow");
    }
    else
    {
        return stack[top--];
    }
}

The removed value is returned and the TOP pointer is decreased by one position.


Implementation of Stack Using Linked List

Stacks can also be implemented using linked lists. Unlike arrays, linked lists do not require a fixed size. Memory is allocated dynamically whenever a new element is inserted.

In a linked-list-based stack, insertion and deletion occur at the beginning of the list.

Advantages of Linked List Implementation


Time Complexity of Stack Operations

Most stack operations are highly efficient because they involve only the top element.

Operation Time Complexity
Push O(1)
Pop O(1)
Peek O(1)
IsEmpty O(1)
IsFull O(1)

Because stack operations require direct access to the top element only, they are extremely fast.


Applications of Stack

Stacks play an important role in many real-world software systems and algorithms.

1. Function Call Management

Whenever a function is called, information such as local variables, parameters, and return addresses is stored in a stack. When the function completes execution, its data is removed from the stack.

2. Recursion

Recursive functions depend heavily on stacks. Each recursive call creates a new stack frame, which is removed after execution.

3. Expression Evaluation

Stacks are used to evaluate arithmetic and logical expressions efficiently.

4. Parentheses Matching

Compilers use stacks to verify whether brackets and parentheses are balanced correctly.

5. Browser Navigation

The Back button in a browser uses a stack to store previously visited pages.

6. Undo and Redo Operations

Text editors and design software use stacks to manage undo and redo actions.

7. Backtracking Algorithms

Many problem-solving techniques use stacks to return to previous states when required.


Recursion and Stack

Recursion is a programming technique where a function calls itself. Every recursive call is stored in the system stack until the base condition is reached.

Example

factorial(3)
factorial(2)
factorial(1)

The stack stores each function call. Once the base condition is reached, execution returns in reverse order.

This behavior perfectly matches the Last In First Out principle.


Expression Evaluation Using Stack

Stacks are commonly used to evaluate arithmetic expressions.

For example:

(10 + 20) * 5

Operators and operands can be stored temporarily in stacks during evaluation.

Many calculators internally use stack-based algorithms to process mathematical expressions.


Parentheses Matching Using Stack

One of the most common applications of stacks is checking balanced parentheses.

Example

{ [ ( ) ] }

Whenever an opening bracket is encountered, it is pushed onto the stack. When a closing bracket appears, the corresponding opening bracket is removed.

If all brackets match correctly, the expression is balanced.


Real-Life Applications of Stack

Web Browsers

Browsers store previously visited pages in a stack. Clicking the Back button retrieves the most recently visited page.

Mobile Applications

Many mobile apps maintain screen navigation using stacks.

Operating Systems

Operating systems use stacks for managing processes and function calls.

Compiler Design

Compilers use stacks for syntax analysis and expression processing.

Gaming Applications

Game engines often use stacks to manage states and navigation between game screens.


Difference Between Stack and Queue

Stack Queue
Follows LIFO. Follows FIFO.
Insertion and deletion at same end. Insertion and deletion at different ends.
Uses TOP pointer. Uses FRONT and REAR pointers.
Last inserted element removed first. First inserted element removed first.

Common Errors While Working with Stack


Stack Interview Questions and Answers

1. What is a Stack?

A Stack is a linear data structure that follows the Last In First Out (LIFO) principle.

2. What are the primary operations of Stack?

Push, Pop, Peek, IsEmpty, and IsFull.

3. What does LIFO mean?

Last In First Out means the most recently inserted element is removed first.

4. What is Stack Overflow?

It occurs when an insertion operation is performed on a full stack.

5. What is Stack Underflow?

It occurs when a deletion operation is performed on an empty stack.

6. What is the time complexity of Push?

O(1)

7. What is the time complexity of Pop?

O(1)

8. Which data structures can implement a stack?

Arrays and Linked Lists.

9. Why is Stack used in recursion?

Because each recursive function call requires temporary storage that follows LIFO behavior.

10. What is the TOP pointer?

It stores the position of the topmost element in the stack.

11. Which operation displays the top element without removing it?

Peek Operation.

12. What is the major advantage of Stack?

Fast insertion and deletion operations.

13. What is the major limitation of Stack?

Only the top element can be accessed directly.

14. How is Stack used in browsers?

It stores browsing history and supports the Back functionality.

15. What is the difference between Stack and Queue?

Stack follows LIFO, while Queue follows FIFO.


Conclusion

Stack is one of the most fundamental and useful data structures in computer science. It follows the Last In First Out principle and provides efficient insertion and deletion operations. Stacks are widely used in recursion, compiler design, operating systems, browser navigation, expression evaluation, and many other applications.

Understanding stack implementation, operations, complexity analysis, and practical applications helps students build a strong foundation in Data Structures and Algorithms. Stack-based problems are frequently asked in technical interviews, competitive programming, and university examinations.

← Previous: Linked List Next: Queue →
Home Visit Our YouTube Channel