-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathP13.java
More file actions
41 lines (32 loc) · 874 Bytes
/
P13.java
File metadata and controls
41 lines (32 loc) · 874 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
package stack_and_queue;
import java.util.Stack;
// Problem Title => Implement a method to insert an element at its bottom without using any other data structure.
public class P13 {
static void insertToBottom(Stack<Integer> s, int n){
Stack<Integer> temp = new Stack<>();
while (!s.empty()){
temp.push(s.peek());
s.pop();
}
s.push(n);
while(!temp.empty()){
s.push(temp.peek());
temp.pop();
}
while(!s.empty()){
System.out.println(s.peek() + " ");
s.pop();
}
}
// Driver function
public static void main(String[] args) {
Stack<Integer> S = new Stack<>();
S.push(5);
S.push(4);
S.push(3);
S.push(2);
S.push(1);
int N = 7;
insertToBottom(S, N);
}
}