C Program to Implement two Stacks using a Single Array and Check for Overflow and Underflow

In this article, we are going to execute a program “C Program to Implement two Stacks using a Single Array and Check for Overflow and Underflow” successfully without facing any troubles. Yes, Let’s do it using this article.

C Program to Implement two Stacks using a Single Array and Check for Overflow and Underflow

C Program to Implement two Stacks using a Single Array and Check for Overflow and Underflow

Program Code

//This is a C Program to Implement two Stacks using a Single Array & Check for Overflow & Underflow
#include <stdio.h>
#define SIZE 10
 
 
int ar[SIZE];
int top1 = -1;
int top2 = SIZE;
 
//Functions to push data
void push_stack1 (int data)
{
  if (top1 < top2 - 1)
  {
    ar[++top1] = data;
  }
  else
  {
    printf ("Stack Full! Cannot Pushn");
  }
}
void push_stack2 (int data)
{
  if (top1 < top2 - 1)
  {
    ar[--top2] = data; 
  }
  else
  {
    printf ("Stack Full! Cannot Pushn");
  }
}
 
//Functions to pop data
void pop_stack1 ()
{
  if (top1 >= 0)
  {
    int popped_value = ar[top1--];
    printf ("%d is being popped from Stack 1n", popped_value);
  }
  else
  {
    printf ("Stack Empty! Cannot Popn");
  }
}
void pop_stack2 ()
{
  if (top2 < SIZE)
  {
    int popped_value = ar[top2++];
    printf ("%d is being popped from Stack 2n", popped_value);
  }
  else
  {
    printf ("Stack Empty! Cannot Popn");
  }
}
 
//Functions to Print Stack 1 and Stack 2
void print_stack1 ()
{
  int i;
  for (i = top1; i >= 0; --i)
  {
    printf ("%d ", ar[i]);
  }
  printf ("n");
}
void print_stack2 ()
{
  int i;
  for (i = top2; i < SIZE; ++i)
  {
    printf ("%d ", ar[i]);
  }
  printf ("n");
}
 
int main()
{
  int ar[SIZE];
  int i;
  int num_of_ele;
 
  printf ("We can push a total of 10 valuesn");
 
  //Number of elements pushed in stack 1 is 6
  //Number of elements pushed in stack 2 is 4
 
  for (i = 1; i <= 6; ++i)
  {
    push_stack1 (i);
    printf ("Value Pushed in Stack 1 is %dn", i);
  }
  for (i = 1; i <= 4; ++i)
  {
    push_stack2 (i);
    printf ("Value Pushed in Stack 2 is %dn", i);
  }
 
  //Print Both Stacks
  print_stack1 ();
  print_stack2 ();
 
  //Pushing on Stack Full
  printf ("Pushing Value in Stack 1 is %dn", 11);
  push_stack1 (11);
 
  //Popping All Elements From Stack 1
  num_of_ele = top1 + 1;
  while (num_of_ele)
  {
    pop_stack1 ();
    --num_of_ele;
  }
 
  //Trying to Pop From Empty Stack
  pop_stack1 ();
 
  return 0;
}

Final Words

We hope You gained a piece of knowledge about “C Program to Implement two Stacks using a Single Array and Check for Overflow and Underflow“. Do visit our other programs too on our website. If you face any difficulties in this topic then ask me via the comment section. Share this article with your friends.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment