forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteMiddleStackElement.java
More file actions
44 lines (33 loc) · 956 Bytes
/
deleteMiddleStackElement.java
File metadata and controls
44 lines (33 loc) · 956 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
import java.io.*;
import java.util.*;
public class deleteMiddleStackElement {
public static void main(String args[]) {
Stack<String> stack = new Stack<String>();
stack.push("1");
stack.push("2");
stack.push("3");
stack.push("4");
stack.push("5");
stack.push("6");
stack.push("7");
stack.push("8");
stack.push("9");
stack.push("10");
stack.push("11");
deleteMiddle(stack, stack.size(), 0);
while (!stack.empty()) {
System.out.print(stack.pop() + "\n");
}
}
static void deleteMiddle(Stack<String> stack, int initialSize, int index) {
// Check for when the stack is empty
if (stack.empty() || index == initialSize)
return;
String element = stack.pop();
// Calls recursive function on modified stack
deleteMiddle(stack, initialSize, index + 1);
// Put all items back except middle
if (index != initialSize / 2)
stack.push(element);
}
}