C to maintain 2 STACKS within a SINGLE ARRAY and the values of one stack should
not overwrite the values of another stack.
/* Maintaining TWO STACKS within a SINGLE ARRAY */
#include<stdio.h>
#include<conio.h>
#define MAX 10
int stack[MAX],top1,top2;
void init()
{
top1=-1;
top2=10;
}
void Push1(int item)
{
stack[++top1]=item;
}
void Push2(int item)
{
stack[--top2]=item;
}
int Pop1()
{
return stack[top1--];
}
int Pop2()
{
return stack[top2++];
}
void Display()
{
int i;
if(top1==-1)
printf("\nStack1 : Empty");
else
{
printf("\nContent of Stack1 :\n");
for(i=0;i<=top1;i++)
printf("%d\t",stack[i]);
}
if(top2==10)
printf("\nStack2 : Empty");
else
{
printf("\nStack2 contains:\n");
for(i=MAX-1;i>=top2;i--)
printf("%d\t",stack[i]);
}
}
void main()
{
int item,ch;
clrscr();
init();
while(1)
{
printf("\n\n\tMenu\n1.Push1\n2.Push2\n3.Pop1\n4.Pop2\n5.Display\n6.Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 : if ((top1 + 1) < top2)
{
printf("\nEnter item to Push into Stack1:");
scanf("%d",&item);
Push1(item);
}
else
printf("\nMemory is Full. Overflow Error");
break;
case 2 : if ((top2 - 1) > top1)
{
printf("\nEnter item to Push into Stack2:");
scanf("%d",&item);
Push2(item);
}
else
printf("\nMemory is Full. Overflow Error");
break;
case 3 : if(top1 <= -1)
printf("\nError : Underflow on pop1");
else
printf("\nPopped item from stack1 is : %d",Pop1());
break;
case 4 : if(top2 >= 10)
printf("\nError : Underflow on pop2");
else
printf("\nPopped item from stack2 is : %d",Pop2());
break;
case 5 : Display();
break;
case 6 : exit(0);
default: printf("\nInvalid Choice");
}
}
}
not overwrite the values of another stack.
/* Maintaining TWO STACKS within a SINGLE ARRAY */
#include<stdio.h>
#include<conio.h>
#define MAX 10
int stack[MAX],top1,top2;
void init()
{
top1=-1;
top2=10;
}
void Push1(int item)
{
stack[++top1]=item;
}
void Push2(int item)
{
stack[--top2]=item;
}
int Pop1()
{
return stack[top1--];
}
int Pop2()
{
return stack[top2++];
}
void Display()
{
int i;
if(top1==-1)
printf("\nStack1 : Empty");
else
{
printf("\nContent of Stack1 :\n");
for(i=0;i<=top1;i++)
printf("%d\t",stack[i]);
}
if(top2==10)
printf("\nStack2 : Empty");
else
{
printf("\nStack2 contains:\n");
for(i=MAX-1;i>=top2;i--)
printf("%d\t",stack[i]);
}
}
void main()
{
int item,ch;
clrscr();
init();
while(1)
{
printf("\n\n\tMenu\n1.Push1\n2.Push2\n3.Pop1\n4.Pop2\n5.Display\n6.Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 : if ((top1 + 1) < top2)
{
printf("\nEnter item to Push into Stack1:");
scanf("%d",&item);
Push1(item);
}
else
printf("\nMemory is Full. Overflow Error");
break;
case 2 : if ((top2 - 1) > top1)
{
printf("\nEnter item to Push into Stack2:");
scanf("%d",&item);
Push2(item);
}
else
printf("\nMemory is Full. Overflow Error");
break;
case 3 : if(top1 <= -1)
printf("\nError : Underflow on pop1");
else
printf("\nPopped item from stack1 is : %d",Pop1());
break;
case 4 : if(top2 >= 10)
printf("\nError : Underflow on pop2");
else
printf("\nPopped item from stack2 is : %d",Pop2());
break;
case 5 : Display();
break;
case 6 : exit(0);
default: printf("\nInvalid Choice");
}
}
}
No comments:
Post a Comment