top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

PROGRAM TO IMPLEMENT MULTIPLE STACK IN A SINGLE ARRAY C LANGUAGE

+1 vote
1,530 views
PROGRAM TO IMPLEMENT MULTIPLE STACK IN A SINGLE ARRAY C LANGUAGE
posted Sep 29, 2016 by Sathyasree

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Think about the memory layout of a process, if you will analyse that then stack grows upwards and heap grows downwards so similar concept you can use here to implement two stacks in a single array

---------------------Array index 0

             -
             -
             -
             -   top 1 (Points to top of stack one)

Space to insert new elements

             - top2 (Points to top of stack two)
             -

----------------------Array Index N

answer Sep 30, 2016 by Sachidananda Sahu
0 votes
#include <STDIO.H>
#define MAX 10

int stack[MAX],topA=-1,topB=MAX;
void pushA(int no)
{
if(topA==topB)
{
printf("\n OVERFLOW");
return;
}
stack[++(topA)]=no;
}
int popA()
{
if(topA==-1)
{
printf("\n UNDERFLOW");
return -999;
}
return stack[(topA)--];
}



void showA()
{
int i;
if(topA==-1)
{
printf("\n stack Empty");
return;
}
for(i=topA;i>=0;i--)
{
printf("\n %d",stack[i]);
}
}

void pushB(int no)
{
if(topB-1==topA)
{
printf("\n OVERFLOW");
return;
}
stack[--(topB)]=no;
}


int popB()
{
if(topB==MAX)
{
printf("\n UNDERFLOW");
return -999;
}
return stack[(topB)--];
}

void showB()
{
int i;
if(topB==MAX)
{
printf("\n stack Empty");
return;
}
for(i=topB;i
{
printf("\n %d",stack[i]);
}
}
answer Sep 29, 2016 by Shivaranjini
...