-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeleteMiddle.java
39 lines (37 loc) · 1.04 KB
/
DeleteMiddle.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
package Stack;
import java.util.Vector;
public class DeleteMiddle {
public static void main(String[] args) {
Stack<Character> st = new Stack<Character>();
st.push('1');
st.push('2');
st.push('3');
st.push('4');
st.push('5');
st.push('6');
st.push('7');
Vector<Character> v = new Vector<Character>();
while (!st.empty()) {
v.add(st.pop());
}
int n = v.size();
if (n % 2 == 0) {
int target = (n / 2);
for (int i = 0; i < n; i++) {
if (i == target) continue;
st.push(v.get(i));
}
} else {
int target = (int) Math.ceil(n / 2);
for (int i = 0; i < n; i++) {
if (i == target) continue;
st.push(v.get(i));
}
}
System.out.print("Printing stack after deletion of middle: ");
while (!st.empty()) {
char p = st.pop();
System.out.print(p + " ");
}
}
}