-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackTest.java
More file actions
88 lines (75 loc) · 2.88 KB
/
StackTest.java
File metadata and controls
88 lines (75 loc) · 2.88 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.util.List;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.Charset;
import java.io.IOException;
import datastructure.list.Stack;
import datastructure.list.ArrayStack;
import datastructure.list.DoubleEndedList;
import java.util.NoSuchElementException;
public class StackTest {
private static List<String> readCommands(String filename) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(filename), Charset.defaultCharset());
// Parsing file formant
for(String line: lines) {
String[] cmd = line.split(" ");
if(!cmd[0].equals("PUSH") && !cmd[0].equals("POP") && !cmd[0].equals("TOP"))
throw new IOException("Invalid command in line: " + line);
if((cmd[0].equals("PUSH") && cmd.length != 2) || (!cmd[0].equals("PUSH") && cmd.length != 1))
throw new IOException("Invalid file format: " + line);
if(cmd[0].equals("PUSH")) {
try {
Integer.parseInt(cmd[1]);
} catch(Exception e) {
throw new IOException("Invalid key format: " + line);
}
}
}
return lines;
}
public static void main(String args[]) {
List<String> lines = null;
if(args.length != 1) {
System.err.println("Usage: StackTest <filename>\n");
System.exit(0);
}
try {
lines = readCommands(args[0]);
} catch(Exception e) {
System.err.println(e);
System.exit(0);
}
//Stack<Integer> S = new ArrayStack<>();
Stack<Integer> S = new DoubleEndedList<>();
for(String l: lines) {
String[] cmd = l.split(" ");
Integer data;
switch(cmd[0]) {
case "PUSH":
data = Integer.parseInt(cmd[1]);
S.push(data);
System.out.println("Pushing " + data + "\n");
break;
case "POP":
try {
data = S.pop();
System.out.println("Popping -> " + data + "\n");
} catch (NoSuchElementException e) {
System.out.println("Popping -> Empty stack\n");
}
break;
case "TOP":
try {
data = S.top();
System.out.println("Top -> " + data + "\n");
} catch (NoSuchElementException e) {
System.out.println("Top -> Empty stack\n");
}
break;
}
System.out.println("Number of elements: " + S.size());
System.out.println(S);
System.out.println("****************\n");
}
}
}