What is Stack Data Structure? A Complete Tutorial (original) (raw)

Last Updated : 06 Mar, 2025

Try it on GfG Practice redirect icon

**Stack is a linear data structure that follows **LIFO (Last In First Out) Principle, the last element inserted is the first to be popped out. It means both insertion and deletion operations happen at one end only.

What-is-Stack-(1)

**LIFO(Last In First Out) Principle

Here are some real world examples of LIFO

Representation of Stack Data Structure:

Stack follows LIFO (Last In First Out) Principle so the element which is pushed last is popped first.

Stack-representation-in-Data-Structures-(1)

**Types of Stack:

Basic Operations on Stack:

In order to make manipulations in a stack, there are certain operations provided to us.

To implement stack, we need to maintain reference to the top item.

**Push Operation on Stack

Adds an item to the stack. If the stack is full, then it is said to be an Overflow condition.

Algorithm for Push Operation:

Push-Operation-in-Stack-(1)

**Pop Operation in Stack

Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.

**Algorithm for Pop Operation:

Pop-Operation-in-Stack-(1)

**Top or Peek Operation on Stack

Returns the top element of the stack.

**Algorithm for Top Operation:

Top-or-Peek-Operation-in-Stack-(1)

**isEmpty Operation in Stack Data Structure:

Returns true if the stack is empty, else false.

**Algorithm for isEmpty Operation:

isEmpty-Operation-in-Stack-(1)

isFull **Operation in Stack **Data Structure:

Returns true if the stack is full, else false.

**Algorithm for isFull Operation:

isFull-Operation-in-Stack-(1)

Implementation of Stack

The basic operations that can be performed on a stack include push, pop, and peek. There are two ways to implement a stack -

**Complexity Analysis of Operations on Stack Data Structure:

Operations Time Complexity Space Complexity
push() O(1) O(1)
pop() O(1) O(1)
top() or peek() O(1) O(1)
isEmpty() O(1) O(1)
isFull() O(1) O(1)

**Next Articles: