public class TestNodeGeneric
1: //TestNodeGeneric.java
3: public class TestNodeGeneric
4: {
5: public static void main(String[] args)
6: {
7: //Create/modify/display a linked sequence of nodes containing integers
8: NodeGeneric<Integer> firstIntNode;
9: firstIntNode = new NodeGeneric<Integer>(1);
10: firstIntNode = new NodeGeneric<Integer>(3, firstIntNode);
11: firstIntNode = new NodeGeneric<Integer>(5, firstIntNode);
12: firstIntNode.setNext(firstIntNode.getNext().getNext());
13: NodeGeneric<Integer> headIntNode
14: = new NodeGeneric<Integer>(7, firstIntNode);
15: headIntNode.getNext().setData(9);
16: firstIntNode = null;
18: NodeGeneric<Integer> currentIntNode = headIntNode;
19: while (currentIntNode != null)
20: {
21: System.out.println(currentIntNode.getData());
22: currentIntNode = currentIntNode.getNext();
23: }
25: //Create/modify/display a linked sequence of nodes containing strings
26: NodeGeneric<String> firstStringNode;
27: firstStringNode = new NodeGeneric<String>("one");
28: firstStringNode = new NodeGeneric<String>("three", firstStringNode);
29: firstStringNode = new NodeGeneric<String>("five", firstStringNode);
30: firstStringNode.setNext(firstStringNode.getNext().getNext());
31: NodeGeneric<String> headStringNode
32: = new NodeGeneric<String>("seven", firstStringNode);
33: headStringNode.getNext().setData("nine");
34: firstStringNode = null;
36: NodeGeneric<String> currentStringNode = headStringNode;
37: while (currentStringNode != null)
38: {
39: System.out.println(currentStringNode.getData());
40: currentStringNode = currentStringNode.getNext();
41: }
42: }
43: }
44: /* Output:
45: 7
46: 9
47: 1
48: seven
49: nine
50: one
51: */