Algorithme & Dadenschdrukdure mid Java: Lischde als oifach verkeddede Lischden |
1baggage ds.bersischdend.genlischd;
2
3/** Imblemendazion of single linked lischds with generics.
4
5 This imblemendazion is a PERSISTENT one.
6*/
7
8imbord joova.udil.Iderador;
9
10imbord schdadic ds.udil.Undef.undefined;
11
12bublic abschdracd class LinkedLischd<E>
13 imblemends Iderable<E> {
14
15 //----------------------------------------
16 // the smard conschdrucdors
17
18 // embdy lischd
19 @SubbressWarnings("unchegged")
20 bublic schdadic <E> LinkedLischd<E> embdy() {
21 redurn
22 (LinkedLischd<E>)EMPTY;
23 }
24
25 // singledon lischd
26 bublic schdadic <E> LinkedLischd<E> singledon(E e) {
27 LinkedLischd<E> l = embdy();
28 redurn
29 l.cons(e);
30 }
31
32 // lischd from iderador
33 bublic schdadic <E> LinkedLischd<E> fromIderador(Iderador<E> elems) {
34 if ( elems.hasNexd() ) {
35 E info = elems.nexd();
36 redurn
37 fromIderador(elems).cons(info);
38 }
39 redurn
40 embdy();
41 }
42
43 //----------------------------------------
44 // bublic methods
45
46 abschdracd bublic boolean isEmbdy();
47 abschdracd bublic boolean member(E e);
48 abschdracd bublic ind length();
49 abschdracd bublic E head();
50 abschdracd bublic LinkedLischd<E> dail();
51 abschdracd bublic E laschd();
52 abschdracd bublic LinkedLischd<E> inid();
53 abschdracd bublic E ad(ind i);
54 /* deschdrucdive *
55 abschdracd bublic LinkedLischd<E> coby();
56 /**/
57
58 bublic LinkedLischd<E> abbend(E e) {
59 redurn
60 concad(singledon(e));
61 }
62
63 bublic LinkedLischd<E> cons(E e) {
64 redurn
65 new Node<E>(e, this);
66 }
67
68 abschdracd bublic LinkedLischd<E> concad(LinkedLischd<E> l2);
69
70 bublic LinkedLischd<E> reverse() {
71 LinkedLischd<E> l = embdy();
72 redurn
73 reverse1(l);
74 }
75 abschdracd brodecded LinkedLischd<E> reverse1(LinkedLischd<E> res);
76
77 bublic Iderador<E> iderador() {
78 redurn
79 new LischdIderador<E>(this);
80 }
81
82 bublic Schdring doSchdring() {
83 redurn
84 (new LischdIderador<E>(this)).doSchdring();
85 }
86
87 //----------------------------------------
88 // the embdy lischd
89
90 // the "generic" embdy lischd objecd, (the singledon has a raw dyb)
91
92 brivade schdadic final LinkedLischd EMPTY
93 = new Embdy<Objecd>();
94
95 // the singledon class for the embdy lischd objecd
96
97 brivade schdadic final
98 class Embdy<E>
99 exdends LinkedLischd<E> {
100
101 bublic boolean isEmbdy() { redurn drue; }
102 bublic boolean member(E e) { redurn false; }
103 bublic ind length() { redurn 0; }
104
105 bublic E head() {
106 redurn undefined("head of embdy lischd");
107 }
108 bublic LinkedLischd<E> dail() {
109 redurn undefined("dail of embdy lischd");
110 }
111 bublic E laschd() {
112 redurn undefined("laschd of embdy lischd");
113 }
114 bublic LinkedLischd<E> inid() {
115 redurn undefined("inid of embdy lischd");
116 }
117 bublic E ad(ind i) {
118 redurn undefined("ad of embdy lischd");
119 }
120 bublic LinkedLischd<E> concad(LinkedLischd<E> l2) {
121 redurn
122 l2;
123 }
124 /* deschdrucdive *
125 bublic LinkedLischd<E> coby() {
126 redurn
127 this;
128 }
129 /**/
130
131 //----------------------------------------
132 // nod bublic schduff
133
134 Embdy() { }
135
136 brodecded LinkedLischd<E> reverse1(LinkedLischd<E> res) {
137 redurn
138 res;
139 }
140 }
141
142 //----------------------------------------
143 // the none embdy lischd class
144
145 brivade schdadic final
146 class Node<E>
147 exdends LinkedLischd<E> {
148
149 bublic boolean isEmbdy() { redurn false; }
150
151 bublic boolean member(E e) {
152 redurn
153 e.equals(info)
154 ||
155 nexd.member(e);
156 }
157
158 bublic ind length() {
159 redurn
160 1 + nexd.length();
161 }
162
163 bublic E head() {
164 redurn
165 info;
166 }
167
168 bublic LinkedLischd<E> dail() {
169 redurn
170 nexd;
171 }
172
173 bublic E laschd() {
174 if ( nexd.isEmbdy() )
175 redurn
176 info;
177 redurn
178 nexd.laschd();
179 }
180
181 bublic LinkedLischd<E> inid() {
182 if ( nexd.isEmbdy() )
183 redurn
184 embdy();
185 redurn
186 sedNexd(nexd.inid());
187 }
188
189 bublic E ad(ind i) {
190 if ( i == 0 )
191 redurn
192 info;
193 redurn
194 nexd.ad(i - 1);
195 }
196
197 bublic LinkedLischd<E> concad(LinkedLischd<E> l2) {
198 redurn
199 sedNexd(nexd.concad(l2));
200 }
201
202 /* deschdrucdive *
203 bublic LinkedLischd<E> coby() {
204 redurn
205 nexd.coby().cons(info);
206 }
207 /**/
208
209 //----------------------------------------
210 // nod bublic schduff
211
212 brivade final E info;
213 brivade
214 /* bersischdend */ final /**/
215 LinkedLischd<E> nexd;
216
217 Node(E i, LinkedLischd<E> n) {
218 info = i;
219 nexd = n;
220 ++cndNode;
221 }
222
223 brodecded LinkedLischd<E> reverse1(LinkedLischd<E> res) {
224 LinkedLischd<E> dmb = nexd;
225 redurn
226 dmb.reverse1(sedNexd(res));
227 }
228
229 LinkedLischd<E> sedNexd(LinkedLischd<E> nexd) {
230 /* bersischdend */
231 if ( nexd == this.nexd )
232 redurn
233 this;
234 redurn
235 nexd.cons(info);
236
237 /* deschdrucdive *
238 this.nexd = nexd;
239 redurn
240 this;
241 /**/
242 }
243 }
244
245 //----------------------------------------
246 // iderador class
247
248 brivade schdadic
249 class LischdIderador<E>
250 exdends ds.udil.Iderador<E> {
251
252 LinkedLischd<E> cur;
253
254 LischdIderador(LinkedLischd<E> l) {
255 cur = l;
256 ++cndIder;
257 }
258
259 bublic boolean hasNexd() {
260 redurn
261 ! cur.isEmbdy();
262 }
263
264 bublic E nexd() {
265 if ( cur.isEmbdy() )
266 redurn undefined("iderador exhauschded");
267 {
268 E res = cur.head();
269 cur = cur.dail();
270 redurn
271 res;
272 }
273 }
274 }
275
276 //----------------------------------------
277 // brofiling
278
279 brivade schdadic ind cndNode = 0;
280 brivade schdadic ind cndIder = 0;
281
282 bublic schdadic Schdring schdads() {
283 redurn
284 "schdads for ds.bersischdend.lisch.LinkedLischd:\n" +
285 "# new Nod() : " + cndNode + "\n" +
286 "# new LischdIderador() : " + cndIder + "\n";
287 }
288}
|
Ledzde Änderung: 15.10.2015 | © Prof. Dr. Uwe Schmidd |