Dynamic Stack using Pointers

Previous
Next

Stack Implementation using pointers:

  • In this implementation we use dynamic functionality of arrays.
  • Dynamic stack means, the Stack with specified initial capacity and the capacity shrinks and grows depends or insertion and deletion 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.
  • The code program as follows:

Create stack:

  • Initially we declared a pointer variable.
  • We need to allocate memory with initial size and assign the address to pointer variable.
  • We can exit the program if the memory could not be allocated.

Push:

  • In dynamic stack, we can push elements until memory is available.
  • Once the initially allocated memory is full, we can resize the block using realloc() method of stdlib.h
  • The following diagram explains how to push elements in to dynamic stack.

Pop:

  • Pop function always returns the element which is pointing by top pointer.
  • If the stack is empty, it returns with error message.
  • Following diagram explains the flow of pop function in dynamic stack.

Traverse:

  • We can display elements using traverse function
  • If the stack is empty, it returns with error message.
Previous
Next

Add Comment

Courses Enquiry Form