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).

1  
sclass Chain<A> implements Iterable<A> {
2  
  A element;
3  
  Chain<A> next;
4  
  int size;
5  
  
6  
  *() {}
7  
  *(A *element) { size = 1; }
8  
  *(A *element, Chain<A> *next) {
9  
    size = next != null ? next.size+1 : 1;
10  
  }
11  
  
12  
  toString { ret str(toList()); }
13  
  
14  
  ArrayList<A> toList() {
15  
    ArrayList<A> l = emptyList(size);
16  
    Chain<A> c = this;
17  
    while (c != null) {
18  
      l.add(c.element);
19  
      c = c.next;
20  
    }
21  
    ret l;
22  
  }
23  
  
24  
  // TODO: optimize
25  
  public Iterator<A> iterator() { ret toList().iterator(); }
26  
}

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: 264 / 592
Version history: 1 change(s)
Referenced in: [show references]