-
Notifications
You must be signed in to change notification settings - Fork 2
/
DeleteMiddleElementOfStack.java
44 lines (36 loc) · 1.08 KB
/
DeleteMiddleElementOfStack.java
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
package Recursion;
import java.util.ArrayDeque;
import java.util.Deque;
/**
* @author kalpak
*
* Given a stack with push(), pop(), empty() operations, delete middle of it without using any additional data structure.
*
* Input : Stack[] = [1, 2, 3, 4, 5]
* Output : Stack[] = [1, 2, 4, 5]
*
* Input : Stack[] = [1, 2, 3, 4, 5, 6]
* Output : Stack[] = [1, 2, 4, 5, 6]
*
*/
public class DeleteMiddleElementOfStack {
public static Deque<Integer> deleteMiddle(Deque<Integer> stack, int size, int current) {
if(stack.isEmpty() || current == size)
return stack;
int temp = stack.poll();
stack = deleteMiddle(stack, size, current+1);
if(current != size/2)
stack.push(temp);
return stack;
}
public static void main(String[] args) {
Deque<Integer> stack = new ArrayDeque<>();
stack.push(9);
stack.push(2);
stack.push(7);
stack.push(4);
stack.push(6);
stack = deleteMiddle(stack, stack.size(), 0); // [6, 4, 7, 2, 9]
System.out.println(stack);
}
}