Representation of Stack in Memory
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle, meaning the last element inserted is the first to be removed. Both insertion and deletion operations happen at one end only.

Since the stack follows the LIFO principle, the element that is pushed last is popped first.

Types of Stack:
- Fixed Size Stack: As the name suggests, a fixed size stack has a predefined capacity and cannot grow or shrink dynamically. If the stack is full and an attempt is made to add an element, an overflow error occurs. If the stack is empty and an attempt is made to remove an element, an underflow error occurs.
- Dynamic Size Stack: A dynamic size stack can grow or shrink dynamically. When the stack is full, it automatically increases its size to accommodate a new element, and when it's empty, it reduces its size. This type of stack is typically implemented using a linked list, which allows for easy resizing.
Implementation of Stack Using an Array
A stack is a linear data structure that follows the LIFO principle. To implement a stack using an array, initialize an array and treat its end as the stack’s top. Implement push
(add to end), pop
(remove from end), and peek
(check end) operations, handling cases for an empty or full stack.
Step-by-step approach:
- Initialize an array to represent the stack.
- Use the end of the array to represent the top of the stack.
- Implement
push
(add to end),pop
(remove from end), andpeek
(check end) operations, ensuring to handle empty and full stack conditions.
Output
Implementation of Stack Using a Linked List
To implement a stack using a singly linked list, all operations must follow the LIFO (Last In, First Out) principle, where the most recently added element is the first to be removed. In this approach, each node contains data and a reference to the next node.
We maintain a top
pointer that always points to the most recent (topmost) node in the stack. The key stack operations—push
, pop
, and peek
—are performed using this pointer.
Stack Operations
push()
: Insert a new element at the beginning of the linked list.pop()
: Remove the first element from the linked list.peek()
: Return the top element.display()
: Print all elements in the stack.
Output
Time Complexity: O(1) for all push()
, pop()
, and peek()
operations, as no traversal is needed.
Auxiliary Space: O(n), where n
is the size of the stack.
Implementation of Stack Using Deque
A double-ended queue or deque allows insertion and deletion at both ends. In a stack, we perform insertions and deletions at only one end. We can use either end of the deque (front or back) to implement a stack.

In the implementation below, we use the back (or rear) of the deque for both insertions and deletions. Note that the time complexities of all operations (insertions and deletions at both ends) in a deque are O(1). Hence, the time complexity of the implementation below is O(1).
- In Java, the deque implementation of a stack is preferred over the standard
Stack
class because theStack
class is a legacy class mainly designed for multithreaded environments. - In Python, there is no standard stack class, so we can use either a list or deque. The deque implementation is preferred because it is optimized for insertions and deletions at both ends.
Output
Operations on Stack and Applications
To manipulate a stack, certain standard operations are available:
push()
to insert an element into the stackpop()
to remove an element from the stacktop()
orpeek()
to return the top element of the stackisEmpty()
returns true if the stack is empty, otherwise falseisFull()
returns true if the stack is full, otherwise false
To implement a stack, we need to maintain a reference to the top element.
Push Operation on Stack
Adds an item to the stack. If the stack is full, it results in an overflow condition.
Algorithm for Push Operation:
-
Check if the stack is full.
-
If the stack is full (
top == capacity - 1
), a Stack Overflow occurs and the element cannot be inserted. -
Otherwise, increment
top
by 1 (top = top + 1
) and insert the new element at the top position. -
Elements can be pushed until the stack reaches its capacity.
Pop Operation on Stack
Removes an item from the stack. Elements are popped in the reverse order of their insertion. If the stack is empty, it results in an underflow condition.
Algorithm for Pop Operation:
-
Check if the stack is empty.
-
If the stack is empty (
top == -1
), a Stack Underflow occurs and no element can be removed. -
Otherwise, store the value at
top
, decrementtop
by 1 (top = top - 1
), and return the stored value.
Top or Peek Operation on Stack
Returns the top element of the stack.
Algorithm for Top Operation:
-
Check if the stack is empty.
-
If the stack is empty (
top == -1
), print “Stack is empty”. -
Otherwise, return the element at index
top
.
isEmpty Operation in Stack Data Structure
Returns true if the stack is empty, otherwise false.
Algorithm for isEmpty Operation:
-
Check the value of
top
. -
If
top == -1
, the stack is empty; return true. -
Otherwise, return false.
isFull Operation in Stack Data Structure
Returns true if the stack is full, otherwise false.
Algorithm for isFull Operation:
-
Check the value of
top
. -
If
top == capacity - 1
, the stack is full; return true. -
Otherwise, return false.
Applications of Stacks
- Function calls: Stacks store return addresses during function calls to ensure the program returns to the correct location after execution.
- Recursion: Used to manage local variables and return addresses in recursive calls.
- Expression evaluation: Useful for evaluating expressions in postfix (Reverse Polish) notation.
- Syntax parsing: Helps validate the syntax in programming and formal languages.
- Memory management: Assists in memory allocation and management in operating systems and some languages.
- Problem-solving: Used in solving problems like Next Greater Element, Previous Greater Element, Next Smaller Element, Previous Smaller Element, Largest Rectangle in Histogram, and Stock Span Problem.
Advantages of Stacks:
- Simplicity: Easy to understand and use.
- Efficiency: Push and pop operations take constant time, O(1).
- LIFO behavior: Ensures the most recently added item is removed first, ideal for function calls and expression evaluation.
- Memory efficiency: Uses only as much memory as needed to store current elements.
Disadvantages of Stacks:
- Limited access: Only the top element can be accessed or modified.
- Overflow potential: In fixed-size stacks, inserting too many elements causes overflow and data loss.
- No random access: Elements cannot be accessed in arbitrary order.
- Fixed capacity: Not suitable for scenarios where the required size is highly variable unless implemented dynamically.