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

26
LINES

< > BotCompany Repo | #1027968 // Chain - singly-linked cons list (forward connected)

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

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

sclass Chain<A> implements Iterable<A> {
  A element;
  Chain<A> next;
  int size;
  
  *() {}
  *(A *element) { size = 1; }
  *(A *element, Chain<A> *next) {
    size = next != null ? next.size+1 : 1;
  }
  
  toString { ret str(toList()); }
  
  ArrayList<A> toList() {
    ArrayList<A> l = emptyList(size);
    Chain<A> c = this;
    while (c != null) {
      l.add(c.element);
      c = c.next;
    }
    ret l;
  }
  
  // TODO: optimize
  public Iterator<A> iterator() { ret toList().iterator(); }
}

Author comment

Began life as a copy of #1027931

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: #1027968
Snippet name: Chain - singly-linked cons list (forward connected)
Eternal ID of this version: #1027968/2
Text MD5: bf1ca612bb8ab78e264a128cda0ffe83
Transpilation MD5: 7847dbc762cf3b3d05c780ee011c2742
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-04-21 10:45:54
Source code size: 530 bytes / 26 lines
Pitched / IR pitched: No / No
Views / Downloads: 249 / 572
Version history: 1 change(s)
Referenced in: [show references]