Saturday, November 5, 2011

Reverse a Stack using a queue in C language


Reverse a Stack using a queue in C language 
#include<conio.h>
 #include<stdio.h>
 #define MAX 20
 void show(int stack[],int size,int top)
 {
int i;
for(i=0;i<size;i++)
{
printf("\nValue at %d is %d",top,stack[top]);
top=top-1;
}
 }
 void reverse(int stack[],int qu[],int *t,int *r,int *f)
 {
*f=0;
while(*t>-1)
{
*r=*r+1;
qu[*r]=stack[*t];
*t=*t-1;
}
while(*f<=*r)
{
*t=*t+1;
stack[*t]=qu[*f];
*f=*f+1;
}
 }
 void main()
 {
int size;
int item,t,i,stack[MAX],quee[MAX];
int top=-1,front=-1,rear=-1;
clrscr();
printf("Enter size of stack::");
scanf("%d",&size);
for(i=0;i<size;i++)
{
top=top+1;
printf("Enter value of for position %d ::",top);
scanf("%d",&item);
stack[top]=item;
}
show(stack,size,top);
reverse(stack,quee,&top,&rear,&front);
printf("\nAfter reverse..............");
show(stack,size,top);
getch();
 }

3 comments:

output:
Enter size of stack::3
Enter value of for position 0 ::2
Enter value of for position 1 ::3
Enter value of for position 2 ::4

Value at 2 is 4
Value at 1 is 3
Value at 0 is 2
After reverse..............
Value at 2 is 2
Value at 1 is 3
Value at 0 is 4

Plz send the code for reverse stack using one another stack

Post a Comment

You are most welcome for making comments here. I will consider your view and will reply you soon.