-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMthToLast.java
More file actions
76 lines (67 loc) · 1.61 KB
/
MthToLast.java
File metadata and controls
76 lines (67 loc) · 1.61 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class ElementList {
char ch;
ElementList next;
public ElementList() {
next = null;
}
public char getMthToLast(int m) {
ElementList tmpNode = this;
ElementList mthLastNode;
int i = 0;
while (i < m) {
tmpNode = tmpNode.next;
i++;
}
mthLastNode = this;
while (tmpNode != null) {
mthLastNode = mthLastNode.next;
tmpNode = tmpNode.next;
}
return mthLastNode.ch;
}
public void createList(String str) {
int i;
ElementList tmpNode = this;
ElementList newNode;
tmpNode.ch = str.charAt(0);
for (i = 1; i < str.length(); i++) {
newNode = new ElementList();
newNode.ch = str.charAt(i);
tmpNode.next = newNode;
tmpNode = tmpNode.next;
}
}
}
public class MthToLast {
int m;
public MthToLast(String filename) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
ElementList el;
StringBuffer str = new StringBuffer();
int i;
while ((line = br.readLine()) != null) {
String[] params = line.split("\\s");
m = Integer.parseInt(params[params.length - 1]);
if (m > params.length - 1 || m <= 0) {
continue;
}
el = new ElementList();
for (i = 0; i < params.length - 1; i++) {
str.append(params[i]);
}
el.createList(str.toString());
System.out.println(el.getMthToLast(m));
}
}
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.out.println("Unsupported number of parameters. Exiting.");
System.exit(1);
}
MthToLast mtl = new MthToLast(args[0]);
}
}