-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveMiddleOfStack.java
More file actions
74 lines (45 loc) · 1.72 KB
/
RemoveMiddleOfStack.java
File metadata and controls
74 lines (45 loc) · 1.72 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package CollectionsGeeksForGeeks;
/**
*
* @author Dell
*/
import java.util.*;
import java.io.*;
public class RemoveMiddleOfStack {
public static void RemoveMiddle(Stack s){
int sz = s.size();
System.out.println("the size of stack is :: " + sz);
int mid = sz / 2;
if(sz%2 == 0){
s.remove(mid - 1);
}
else{
s.remove(mid);
}
Iterator itr = s.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testcase = Integer.valueOf(br.readLine());
while(testcase != 0){
//creating new stack in every interation of test cases.
Stack<Integer> st = new Stack<>();
//accepting the value of n elements that are to be inserted into the stack.
int n = Integer.valueOf(br.readLine());
//pushing elements in the stack.
for(int i = 0 ; i < n ; i++){
st.push(Integer.valueOf(br.readLine()));
}
testcase = testcase - 1;
RemoveMiddle(st);
}
}
}