KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Made > CDL > List


1 /* $Id: List.java,v 1.1.1.1 2003/02/11 16:19:40 bures Exp $ */
2 package SOFA.SOFAnode.Made.CDL;
3
4 class ListItem {
5   public Object JavaDoc item;
6   public ListItem prev;
7   public ListItem next;
8   public ListItem(Object JavaDoc what, ListItem prv, ListItem nxt) {
9     item = what;
10     prev = prv;
11     next = nxt;
12   }
13 }
14
15 class List {
16   int count;
17   ListItem first;
18   ListItem last;
19   ListItem akt;
20   
21   public List() {
22     count = 0;
23     first = null;
24     last = null;
25     akt = null;
26   }
27
28   void add(Object JavaDoc what) {
29     ListItem li;
30     if (count==0) {
31       li = new ListItem(what, null, null);
32       first = li;
33       last = li;
34       akt = first;
35     } else {
36       li = new ListItem(what, last, null);
37       last.next = li;
38       last = li;
39     }
40     count++;
41   }
42
43   void prepend(Object JavaDoc what) {
44     ListItem li;
45     if (count==0) {
46       li = new ListItem(what, null, null);
47       first = li;
48       last = li;
49       akt = first;
50     } else {
51       li = new ListItem(what, null, first);
52       first.prev = li;
53       first = li;
54     }
55     count++;
56   }
57
58   public int size() { return count; }
59
60   public Object JavaDoc aktual() {
61     if (akt==null)
62       return null;
63     return akt.item;
64   }
65
66   public boolean toNext() {
67     if (akt.next==null)
68       return false;
69     else akt = akt.next;
70     return true;
71   }
72
73   public boolean toPrev() {
74     if (akt.prev==null)
75       return false;
76     else akt = akt.prev;
77     return true;
78   }
79
80   public void toFirst() { akt = first; }
81
82   public void toLast() {akt = last; }
83
84   public Object JavaDoc removeAkt() {
85     if (count>0) {
86       Object JavaDoc ret = akt.item;
87       if (count==1) {
88     akt = first = last = null;
89     count = 0;
90       } else {
91         if (akt == first) {
92           first = akt.next;
93       first.prev = null;
94       akt.next = null;
95       akt = first;
96     } else {
97           if (last == akt) {
98         last = akt.prev;
99         last.next = null;
100         akt.prev = null;
101         akt = last;
102       } else {
103         akt.prev.next = akt.next;
104         akt.next.prev = akt.prev;
105         akt = akt.next;
106       }
107     }
108         count--;
109       }
110       return ret;
111     }
112     return null;
113   }
114 }
115
Popular Tags