Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

37
LINES

< > BotCompany Repo | #1027931 // ReverseChain - backwards singly-linked cons list

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2518L/16K).

sclass ReverseChain<A> implements Iterable<A> {
  A element;
  ReverseChain<A> prev;
  int size;
  
  *() {}
  *(ReverseChain<A> *prev, A *element) {
    if (prev == null) size = 1;
    else {
      prev.check();
      size = prev.size+1;
    }
  }
  
  void check {
    if (size < 1) fail("You called the ReverseChain default constructor. Don't do that");
  }
  
  toString {
    ret str(toList());
  }
  
  ArrayList<A> toList() {
    check();
    ArrayList<A> l = emptyList(size);
    for i to size: l.add(null);
    int i = size;
    ReverseChain<A> c = this;
    while (c != null) {
      l.set(--i, c.element);
      c = c.prev;
    }
    ret l;
  }
  
  public Iterator<A> iterator() { ret toList().iterator(); }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027931
Snippet name: ReverseChain - backwards singly-linked cons list
Eternal ID of this version: #1027931/18
Text MD5: 1b73c2d1681a287b6a8038c9f44e6d7d
Transpilation MD5: 2c005cdb3cf48e000f09592631322054
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-05-23 17:23:39
Source code size: 757 bytes / 37 lines
Pitched / IR pitched: No / No
Views / Downloads: 358 / 777
Version history: 17 change(s)
Referenced in: #1027968 - Chain - singly-linked cons list (forward connected)
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)