-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay14.java
More file actions
39 lines (39 loc) · 841 Bytes
/
Copy pathDay14.java
File metadata and controls
39 lines (39 loc) · 841 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
import java.util.*;
public class Day14{
static Stack<Integer> s = new Stack<Integer>();
static void insertAtBottom(int data){
if (s.isEmpty()) {
s.push(data);
}
else{
int prevData = s.peek();
s.pop();
insertAtBottom(data);
s.push(prevData);
}
}
static void reverse(){
if (s.isEmpty()) {
return;
}
int data = s.peek();
s.pop();
reverse();
insertAtBottom(data);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of elements");
int size = in.nextInt();
System.out.println("Enter the "+size+" data");
for (int i=0;i<size ;i++ ) {
int num = in.nextInt();
s.push(num);
}
System.out.println("Orignal Stack");
System.out.println(s);
reverse();
System.out.println("Reversed Stack");
System.out.println(s);
}
}