C Implementation of  Handling TWO Stacks simultaneously :- Multiple Stack Handling

/*****C Implementation of  Handling TWO Stacks simultaneously*****/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define size 6

typedef struct
{
int item[size];
int top;
}Stack;

void push(Stack *sp,int val)
{
if(sp->top ==size-1)
printf(“stack overflow\n”);

sp->top++;
sp->item[sp->top]=val;
printf(“the value is pushed\n”);

}

int pop(Stack *sp)
{
int value;

if(sp->top==-1)
//printf(“stact underflow\n”);
return -9999;

value= sp->item[sp->top];
sp->top–;
return value;

}

void display(Stack *sp)
{
int i;
printf(“\nstack\n”);
for(i=sp->top;i>=0;i–)
{
printf(“%d\n”,sp->item[i]);
}
}

void menu(Stack *s)
{

int choice,value;

while(1)

{
printf(” press 1 for Push\n press 2 for pop\n press 3 for display\n press 4 for exit\n\n”);
scanf(“%d”,&choice);

if(choice==4)
break;

switch(choice)
{
case 1:
printf(“enter the value which is to be pushed\n”);
scanf(“%d”,&value);
push(s,value);
break;

case 2:
value= pop(s);
if (value== -9999 && s->top == -1)
printf(“stack underflow\n\n\n”);
else
printf(“the poped data is %d\n”,value);

break;

case 3:
display(s);
break;

case 4:
//exit(0);
break;

default: printf(“invalid choice\n”);

}//end of switch
}//end of while

}

int main()
{
int i;
Stack s1,s2;
s1.top=-1;
s2.top=-1;
clrscr();

while(1)
{
printf(“Press 1 for 1st stack\nPress 2 for 2nd stack\nPress 3 for Exit\n”);
printf(“\nEnter your choice:”);
scanf(“%d”,&i);

switch(i)
{
case 1:
menu(&s1);
break;
case 2:
menu(&s2);
break;
case 3:
exit(0);
// break;

default:
printf(“\ninvalid choice\n”);
}

}

getch();
return 0;
}

/*****C Implementation of  Handling TWO Stacks simultaneously*****/

C Implementation of  Handling Multiple Stacks simultaneously from user choice : Handling Multiple Stacks from user choice

/*****C Implementation of  Handling Multiple Stacks simultaneously*****/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define size 6

typedef struct
{
int item[size];
int top;
}Stack;

void push(Stack *sp,int val)
{
if(sp->top ==size-1)
printf(“\nstack overflow\n”);

sp->top++;
sp->item[sp->top]=val;
printf(“\nthe value is pushed\n”);

}

int pop(Stack *sp)
{
int value;

if(sp->top==-1)
//printf(“stact underflow\n”);
return -9999;

value= sp->item[sp->top];
sp->top–;
return value;

}

void display(Stack *sp)
{
int i;
printf(“\nstack\n”);
for(i=sp->top;i>=0;i–)
{
printf(“\n%d\n”,sp->item[i]);
}
}

void menu(Stack *s)
{

int choice,value;

while(1)

{
printf(“\n press 1 for Push\n press 2 for pop\n press 3 for display\n press 4 for exit\n\n”);
printf(“\nEnter your choice:”);
scanf(“%d”,&choice);

if(choice==4)
break;

switch(choice)
{
case 1:
printf(“\nEnter the value which is to be pushed\n”);
scanf(“%d”,&value);
push(s,value);
break;

case 2:
value= pop(s);
if (value== -9999) //|| s->top == -1)
printf(“\nstack underflow\n\n\n”);
else
printf(“\nthe poped data is %d\n”,value);

break;

case 3:
display(s);
break;

case 4:
//exit(0);
break;

default: printf(“\ninvalid choice\n”);

}//end of switch
}//end of while

}

int main()
{
int i,nos;
Stack *s;

clrscr();

printf(“\nenter how many stack you want to create:”);
scanf(“%d”,&nos);
s=(Stack *)malloc(size*nos);

for(i=1;i<=nos; i++)
s[i].top=-1;

while(1)
{
printf(“\nEnter the stack number on which you want to work with\nenter 0 for exit”);
printf(“\nEnter your choice:”);
scanf(“%d”,&i);

if(i==0)
exit(0);

else if(i>nos){
printf(“\nInvalid stack number\n\n”);
continue;
}

else
menu(&s[i]);

}

return 0;
//getch();
}
/*****C Implementation of  Handling Multiple Stacks simultaneously*****/

Advertisements