public class LinkedList2
1: //LinkedList2.java
3: public class LinkedList2<E>
4: {
5: private ListNode2<E> head;
6:
7: public LinkedList2()
8: {
9: head = null;
10: }
11:
12: public void showList()
13: {
14: ListNode2<E> position = head;
15: while (position != null)
16: {
17: System.out.println(position.getData());
18: position = position.getLink();
19: }
20: }
21:
22: public int length()
23: {
24: int count = 0;
25: ListNode2<E> position = head;
26: while (position != null)
27: {
28: count++;
29: position = position.getLink();
30: }
31: return count;
32: }
33:
34: public void addANodeToStart
35: (
36: E addData
37: )
38: {
39: head = new ListNode2<E>(addData, head);
40: }
41:
42: public void deleteHeadNode()
43: {
44: if (head != null)
45: {
46: head = head.getLink();
47: }
48: else
49: {
50: System.out.println("Deleting from an empty list.");
51: System.exit(0);
52: }
53: }
54:
55: public boolean onList
56: (
57: E target
58: )
59: {
60: return find(target) != null;
61: }
62:
63: private ListNode2<E> find
64: (
65: E target
66: )
67: {
68: boolean found = false;
69: ListNode2<E> position = head;
70: while (position != null)
71: {
72: E dataAtPosition = position.getData();
73: if (dataAtPosition.equals(target))
74: found = true;
75: else
76: position = position.getLink();
77: }
78: return position;
79: }
80: }