-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackImplementation.java
More file actions
61 lines (47 loc) · 1.07 KB
/
Copy pathStackImplementation.java
File metadata and controls
61 lines (47 loc) · 1.07 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
/**
*Write a program implementing a stack inteface for integers.The interface should have 'push' and
*'pop' functions. You will be asked to 'push' a series of integers and then 'pop' and print out
*every alternate integer.
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;
public class stack {
static Stack<Integer> s = new Stack<Integer>();
static int size;
public static void main(String[] args) {
String ar = args[0];
read(ar);
}
public static void read(String f) {
File file = new File(f);
try {
Scanner in = new Scanner(file);
while (in.hasNextLine()) {
String s = in.nextLine();
pop(s);
}
} catch (FileNotFoundException e) {
}
}
private static void pop(String r) {
Scanner sc = new Scanner(r);
while (sc.hasNext()) {
s.push(sc.nextInt());
}
size = s.size();
push();
}
private static void push() {
String st = "";
for (int i = 0; i < size; i++) {
if (i % 2 == 0) {
st += s.pop() + " ";
} else {
s.pop();
}
}
System.out.println(st);
}
}