-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRevStkAry.java
More file actions
62 lines (45 loc) · 864 Bytes
/
RevStkAry.java
File metadata and controls
62 lines (45 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/// WAP to reverse a string or Array using Stack
import java.util.*;
public class RevStkAry
{
public static void main(String[] args)
{
int top=0,n,i;
Scanner s=new Scanner(System.in);
System.out.println("Enter the size Array! ");
n=s.nextInt();
int a[]=new int[n];
int stack[]=new int[n];
System.out.print("Enter the Array element! \t");
for( i=0;i<=n-1;i++)
{
a[i]=s.nextInt();
}
// Create a loop for push function
for(i=0;i<n;i++)
{
stack[top]=a[i];
if(top==(n-1))
{
break;
}
top++;
}
// create a loop for Pop fuction
for(i=0;i<n;i++)
{
a[i]=stack[top];
if(top<0)
{
break;
}
top--;
}
// now print array and get reverse order
System.out.print("Reverse Array :! \t");
for(i=0;i<n;i++)
{
System.out.print(" "+(a[i]));
}
}
}