Filas y colas
Publicado por Andrea (4 intervenciones) el 27/08/2018 01:56:37
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
/*
* LinkedStack.java
*
*/
public class LinkedStack {
class Node {
Object elem;
Node Next;
public Node(Object o) {
elem = o;
Next = null;
}
}
Node end;
int size;
public LinkedStack() {
end = null;
size = 0;
}
public void push(Object o) {
Node new_node = new Node(o);
if (end == null)
end = new_node;
else {
new_node.Next = end;
end = new_node;
}
size++;
}; // inserts an object onto the stack
public Object pop() {
if (end == null)
return null;
;
Object o = end.elem;
end = end.Next;
size--;
return o;
}// Gets the object from the stack
public boolean isEmpty() {
return (size == 0);
}
public int size() {
return size;
}
public Object end() {
if (end == null)
return null;
else
return end.elem;
}
} // class LinkedStack
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
import java.io.*;
public class TestLinkedStack {
public static void main(String args[]) {
FileReader f = null; // to read files
BufferedReader reader = null; // reading buffer
String line = null; // read lines
LinkedStack stack = new LinkedStack(); // initialization
if (args.length < 1) {
System.err.println("Please invoke the program like this:");
System.err.println("\tLinkedStack file");
}
// opens the file
try {
f = new FileReader(args[0]);
reader = new BufferedReader(f);
while ((line = reader.readLine()) != null)
stack.push(line);
} catch (Exception e) {
System.err.println("File not found " + args[0]);
return;
}
// Gets the strings from the stack and prints them
while ((line = (String) stack.pop()) != null) {
System.out.println(line);
}
}
}
Valora esta pregunta


0