Implementation of multi stack using C in dataStructure

Updated:13/01/2024 by Computer Hope

Go Back

Using more than one stack in program with single Array it is called multi-stack

When a stack is created using single array, we can not able to store large amount of data, thus this problem is rectified using more than one stack in the same array of sufficient array. This technique is called as Multiple Stack.
When an array of STACK[n] is used to represent two stacks, say Stack A and Stack B. Then the value of n is such that the combined size of both the Stack[A] and Stack[B] will never exceed n. Stack[A] will grow from left to right, whereas Stack[B] will grow in opposite direction ie) right to left.

What is  hive , Hive table , hive query spark hadoop big data

           //WAP to implement multiple stack 
# define MAX 10
int stack[MAX], topA = -1,topB=MAX;
void  pushA (int  val)
  {  if (topA+1 == topB)
	 printf("\noverflow in StackA");
	 return ;  }
   stack [++topA]=val;
int  popA( )
  {  if(topA==-1)                                                 
        printf("\nunderflow in StackA");
        return -999; }
     return stack[topA--];

void showA ( )
   {  int i;
        printf ("\nstackA is empty");
        return;  }
      for (i=topA; i>=0; i--)
	  printf("   %d", stack[i]);

void  pushB (int  val)
  { if (topB-1 == topA)
        printf("\noverflow in StackB");
        return ; }
   stack [--topB]=val;

int  popB( )
  { if(topB==MAX)
       printf("\nunderflow in StackB");
       return -999; }
  return stack[topB++];

void showB ( )
   { int i;
         printf ("\nstackB is empty");
         return; }
      for (i=topB; i<MAX; i++)
	  printf("   %d", stack[i]);

void main ()
    { int no, ch;
        {	printf ("\n 1 pushA");
	printf ("\n 2 popA");
	printf ("\n 3 showA");
	printf ("\n 4 pushB");
	printf ("\n 5 popB");
	printf ("\n 6 showB");
	printf ("\n 0 exit");
	printf ("\n enter your Choice:");
	scanf ("%d", &ch);
	switch (ch)
	 {case 1 : printf("\n Enter no : ");
		  scanf("%d", &no);
	  case 2 : no = popA( );
		  if (no != -999)
		     printf ("\n % d poped ",no);
	  case 3 : showA();
	  case 4 : printf("\n Enter no : ");
		  scanf("%d", &no);
	  case 5 : no = popB( );
		  if (no != -999)
		     printf ("\n % d poped ",no);
	  case 6 : showB();
	  case 0 : break;
	  default : printf("\n invalid choice");
	  }//end of switch
         }while ( ch !=  0);



In this Article , We used array int stack[MAX] and manage multiple stacks within a single array. Here's a basic example of how you could implement a multi-stack.
This example uses a single array to store the elements of all stacks. The push and pop operations take into account the stack number to determine the appropriate location in the array for the given stack. The function allocates memory for the array and initializes the top of the stacks.