Problem Statement :
Program to Implement two Stacks using a Single Array & Check for Overflow & Underflow
My code:
//Here is my solution to this question in C language
//I have taken the size of array containing STACK 1 and STACK 2 as 10. Readers may change it as per their need
#include <stdio.h>
#include <stdlib.h>
#define size 10
int arr[size];
int top1=-1;
int top2=size;
//push() for STACK 1:
void push_stack_1(int value)
{
if(top1<top2-1)
{
top1=top1+1;
arr[top1]=value;
}
else
{
printf("STACK 1 OVERFLOW\n");
}
}
//push() for STACK 2:
void push_stack_2(int value)
{
if(top1<top2-1)
{
top2=top2-1;
arr[top2]=value;
}
else
{
printf("STACK 2 OVERFLOW\n");
}
}
//pop() for STACK 1:
void pop_stack_1()
{
if(top1>=0)
{
int popped_value=arr[top1];
top1=top1-1;
printf("%d is popped from STACK 1\n",popped_value);
}
else
{
printf("STACK 1 UNDERFLOW\n");
}
}
//pop() for STACK 2:
void pop_stack_2()
{
if(top2<size)
{
int popped_value=arr[top2];
top2=top2+1;
printf("%d is popped from STACK 2\n",popped_value);
}
else
{
printf("STACK 2 UNDERFLOW\n");
}
}
//Displaying
//display() for STACK 1:
void display_stack_1()
{
int i;
printf("The values of STACK 1 in LIFO manner is:\n");
for(i=top1;i>=0;i--)
{
printf("%d\n",arr[i]);
}
}
//display() for STACK 2:
void display_stack_2()
{
int i;
printf("The values of STACK 2 in LIFO manner is:\n");
for(int i=top2;i<size;i++)
{
printf("%d\n",arr[i]);
}
}
int main()
{
int arr[size];
int i;
int num_of_ele;
printf ("We can push a total of 10 values\n");
printf("-----------------------------------------------------------");
printf("\n");
//Number of elements pushed in stack 1 is 5
//Number of elements pushed in stack 2 is 5
// loop to insert the elements into Stack1
for (i = 1; i <= 5; i++)
{
push_stack_1(i);
printf ("Value Pushed in Stack 1 is %d\n", i);
}
printf("-----------------------------------------------------------");
printf("\n");
// loop to insert the elements into Stack2.
for (i = 6; i <= 10; i++)
{
push_stack_2(i);
printf ("Value Pushed in Stack 2 is %d\n", i);
}
//Print Both Stacks
display_stack_1();
printf("-----------------------------------------------------------");
printf("\n");
display_stack_2 ();
//Pushing on Stack Full
printf ("Pushing Value in Stack 1 is %d\n", 11);
push_stack_1 (11);
printf ("Pushing Value in Stack 2 is %d\n", 110);
push_stack_2 (110);
printf("-----------------------------------------------------------");
printf("\n");
//Popping All Elements from Stack 1
num_of_ele = top1 + 1;
while (num_of_ele)
{
pop_stack_1 ();
num_of_ele--;
}
printf("-----------------------------------------------------------");
printf("\n");
num_of_ele = top2 + 1;
while (num_of_ele)
{
pop_stack_2 ();
num_of_ele--;
}
// Trying to Pop the element From the Empty Stack
printf("-----------------------------------------------------------");
printf("\n");
printf("Trying to Pop the element From the Empty Stack 1\n");
pop_stack_1 ();
printf("-----------------------------------------------------------");
printf("\n");
printf("Trying to Pop the element From the Empty Stack 2\n");
pop_stack_2 ();
return 0;
}
No comments:
Post a Comment