DS – Stack Using Pointers

Stack using Pointers

Dynamic Stack:

  • Dynamic stack means, the Stack with specified initial capacity and the capacity shrinks and grows depends or insertions or deletions of elements.
  • We use DMA functionality of stdlib.h header file such as calloc(), realloc() and free()
  • Dynamic stack operations can be performed using Pointers.
  • Stack and TOP are pointer type variables in Dynamic Stack implementation.

Create Stack:

  • Every stack must be created with initial capacity.
  • We read the size from the end user and create Stack using calloc() function.

Insert element into Stack :

  • Insert elements up to its initial capacity.
  • Increase the size of stack by one using realloc() function from its initial capacity.
  • With every new insertion, the capacity increase by 1.

Logic to display elements of Stack:

  • If Stack is Empty, we return with a message
  • If Stack is not empty, we need to display all elements from top to stack

Logic to delete elements from Stack:

  • If the Stack is empty, we return with Error message
  • If Stack has elements, we remove the top element.
  • We need to maintain the minimum capacity of Stack that has specified at the time of creation.
Scroll to Top