Basic Operations on Stack Data Structure

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

  • push() to insert an element into the stack
  • pop() to remove an element from the stack
  • top() Returns the top element of the stack.
  • isEmpty() returns true if stack is empty else false.
  • isFull() returns true if the stack is full else false.

Push Operation in Stack Data Structure:

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

Algorithm for Push Operation:

  • Before pushing the element to the stack, we check if the stack is full .
  • If the stack is full (top == capacity-1) , then Stack Overflows and we cannot insert the element to the stack.
  • Otherwise, we increment the value of top by 1 (top = top + 1) and the new value is inserted at top position .
  • The elements can be pushed into the stack till we reach the capacity of the stack.

Pop Operation in Stack Data Structure:

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:

  • Before popping the element from the stack, we check if the stack is empty .
  • If the stack is empty (top == -1), then Stack Underflows and we cannot remove any element from the stack.
  • Otherwise, we store the value at top, decrement the value of top by 1 (top = top – 1) and return the stored top value.

Top or Peek Operation in Stack Data Structure:

Returns the top element of the stack.

Algorithm for Top Operation:

  • Before returning the top element from the stack, we check if the stack is empty.
  • If the stack is empty (top == -1), we simply print “Stack is empty”.
  • Otherwise, we return the element stored at index = top .

isEmpty Operation in Stack Data Structure:

Returns true if the stack is empty, else false.

Algorithm for isEmpty Operation:

  • Check for the value of top in stack.
  • If (top == -1) , then the stack is empty so return true .
  • Otherwise, the stack is not empty so return false .

isFull Operation in Stack Data Structure:

Returns true if the stack is full, else false.

Algorithm for isFull Operation:

  • Check for the value of top in stack.
  • If (top == capacity-1), then the stack is full so return true.
  • Otherwise, the stack is not full so return false.

What is Stack Data Structure? A Complete Tutorial

Stack Data Structure is a linear data structure that follows LIFO (Last In First Out) Principle , so the last element inserted is the first to be popped out. In this article, we will cover all the basics of Stack, Operations on Stack, its implementation, advantages, disadvantages which will help you solve all the problems based on Stack.

Table of Content

  • What is Stack Data Structure?
  • Representation of Stack Data Structure:
  • Types of Stack Data Structure:
  • Basic Operations on Stack Data Structure
    • Push Operation in Stack Data Structure
    • Pop Operation in Stack Data Structure
    • Top or Peek Operation in Stack Data Structure
    • isEmpty Operation in Stack Data Structure
    • isFull Operation in Stack Data Structure
  • Implementation of Stack Data Structure
    • Implementation of Stack Data Structure using Array
    • Implementation of Stack Data Structure using Linked List
  • Complexity Analysis of Operations on Stack Data Structure
  • Advantages of Stack Data Structure
  • Disadvantages of Stack Data Structure
  • Applications of Stack Data Structure

Similar Reads

What is Stack Data Structure?

Stack is a linear data structure based on LIFO(Last In First Out) principle in which the insertion of a new element and removal of an existing element takes place at the same end represented as the top of the stack....

Representation of Stack Data Structure:

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

Types of Stack Data Structure:

Fixed Size Stack : As the name suggests, a fixed size stack has a fixed size and cannot grow or shrink dynamically. If the stack is full and an attempt is made to add an element to it, an overflow error occurs. If the stack is empty and an attempt is made to remove an element from it, 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 the new element, and when the stack is empty, it decreases its size. This type of stack is implemented using a linked list, as it allows for easy resizing of the stack....

Basic Operations on Stack Data Structure:

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

Implementation of Stack Data Structure:

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)...

Advantages of Stack Data Structure:

Simplicity: Stacks are a simple and easy-to-understand data structure, making them suitable for a wide range of applications. Efficiency: Push and pop operations on a stack can be performed in constant time (O(1)) , providing efficient access to data. Last-in, First-out (LIFO): Stacks follow the LIFO principle, ensuring that the last element added to the stack is the first one removed. This behavior is useful in many scenarios, such as function calls and expression evaluation. Limited memory usage: Stacks only need to store the elements that have been pushed onto them, making them memory-efficient compared to other data structures....

Disadvantages of Stack Data Structure:

Limited access: Elements in a stack can only be accessed from the top, making it difficult to retrieve or modify elements in the middle of the stack. Potential for overflow: If more elements are pushed onto a stack than it can hold, an overflow error will occur, resulting in a loss of data. Not suitable for random access: Stack s do not allow for random access to elements, making them unsuitable for applications where elements need to be accessed in a specific order. Limited capacity: Stacks have a fixed capacity, which can be a limitation if the number of elements that need to be stored is unknown or highly variable....

Applications of Stack Data Structure:

Infix to Postfix /Prefix conversion Redo-undo features at many places like editors, photoshop. Forward and backward features in web browsers In Memory management, any modern computer uses a stack as the primary management for a running purpose. Each program that is running in a computer system has its own memory allocations. Stack also helps in implementing function call in computers. The last called function is always completed first....

Contact Us