How to reverse the elements of an array using stack in java?
Answers
Answered by
0
Answer:
Explanation:
To reverse an array using stack initially push all elements in to the stack using the push() method then, retrieve them back using the pop() method into another array.
Answered by
0
Answer:
Explanation:
reversing an array of elements using a Stack in C
#include<stdio.h>
int stack[20];
int top=-1;
void push(int elem)
{
top=top+1;
stack[top]=elem;
}
int pop()
{
int t = stack[top];
top=top-1;
return t;
}
int main()
{
int i,n,a[20];
printf("enter the range of elements :");
scanf("%d",&n);
printf("enter the elements of the list:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
push(a[i]);
}
printf("Reversing array as below and printing : ");
for(i=0;i<n;i++)
{
a[i]=pop();
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
Similar questions