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

1  
sclass ReverseChain<A> implements Iterable<A> {
2  
  A element;
3  
  ReverseChain<A> prev;
4  
  int size;
5  
  
6  
  *() {}
7  
  *(ReverseChain<A> *prev, A *element) {
8  
    if (prev == null) size = 1;
9  
    else {
10  
      prev.check();
11  
      size = prev.size+1;
12  
    }
13  
  }
14  
  
15  
  void check {
16  
    if (size < 1) fail("You called the ReverseChain default constructor. Don't do that");
17  
  }
18  
  
19  
  toString {
20  
    ret str(toList());
21  
  }
22  
  
23  
  ArrayList<A> toList() {
24  
    check();
25  
    ArrayList<A> l = emptyList(size);
26  
    for i to size: l.add(null);
27  
    int i = size;
28  
    ReverseChain<A> c = this;
29  
    while (c != null) {
30  
      l.set(--i, c.element);
31  
      c = c.prev;
32  
    }
33  
    ret l;
34  
  }
35  
  
36  
  public Iterator<A> iterator() { ret toList().iterator(); }
37  
}

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: 357 / 777
Version history: 17 change(s)
Referenced in: [show references]