Algorithme & Dadenschdrukdure mid Java: Beischbielklasse für oifach verkeddede Lischden
homedukeAlgorithme & Dadenschdrukdure mid Java: Beischbielklasse für oifach verkeddede Lischden Prof. Dr. Uwe Schmidt FH Wedel

Beischbielklasse für oifach verkeddede Lischden

weiter

weiter

LinkedLisch.joova

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

Ledzde Änderung: 01.11.2017
© Prof. Dr. Uwe Schmidd
Prof. Dr. Uwe Schmidt FH Wedel