Stack Data Structure

Stack is a fundamental linear data structure that operates based on a specific order of operations. This structure adheres to either LIFO (Last In First Out) or FILO (First In Last Out) principles. In LIFO, the last element inserted is the first one to be removed, while in FILO, the first element inserted is the last one to be removed.

Understanding the Stack Data Structure

A stack represents a linear data structure that strictly follows the Last-In-First-Out (LIFO) concept. It can be likened to a stack of plates, where the most recently added plate is the first to be taken out. This characteristic distinguishes stacks from other data structures and plays a crucial role in various computing applications.d.

Basic Operations of Stack Data Structures:

  • Push: This operation adds an element to the top of the stack.

  • Pop: When this operation is executed, it removes the top element from the stack.

  • Peek: By using this operation, you can retrieve the top element without removing it.

  • IsEmpty: This operation checks whether the stack is empty or not.

  • IsFull: In the case of fixed-size arrays, this operation checks if the stack is full.

Understanding the Working of Stack

The stack operates based on the Last-In-First-Out (LIFO) principle. In the illustration below, you can see a stack consisting of five memory blocks, indicating that its size is limited to 5.

Let's consider a scenario where we aim to store elements in a stack, starting with an empty stack. The stack size is set to 5, and as demonstrated below, we progressively push elements into the stack until it reaches its maximum capacity.

DS Stack Introduction

When we engage in the delete operation on a stack, the process involves a single entry and exit point due to the closure of the other end. This adheres to the Last In First Out (LIFO) pattern, meaning that the first value entered will be the last one removed. In the scenario mentioned earlier, where the value 5 is the initial entry, it will only be removed after all other elements have been deleted.

PUSH Operation

The PUSH operation entails the following steps:

  • Before adding an element to a stack, we verify if the stack has reached its full capacity.

  • If an attempt is made to insert an element into a full stack, it triggers an overflow situation.

  • Upon initializing a stack, the top value is initially set to -1 to indicate an empty stack.

  • When a new element is pushed into the stack, the top value is incremented by one, i.e., top = top + 1, and the element is placed at the new top position.

  • Elements continue to be inserted until the stack reaches its maximum size.

POP Operation

The POP operation involves the following steps:

  • Prior to removing an element from the stack, we verify if the stack is empty.

  • If an attempt is made to delete an element from an empty stack, it results in an underflow condition.

  • If the stack is not empty, we first access the element pointed to by the top.

  • Upon completion of the pop operation, the top value is decremented by 1, i.e., top = top - 1.

Applications of Stack Data Structures:

  • Recursion

  • Expression Evaluation and Parsing

  • Depth-First Search (DFS)

Easy Problems on Stack Data Structures:

Medium Problems on Stack Data Structures:

Hard Problems on Stack Data Structures: