1 2 3 module llist.slist; 4 5 6 7 struct Node(T) { 8 // A node with the next pointer of `null` is a "Sentinel node". 9 Node!T* next; 10 11 T payload; 12 alias value = payload; 13 } 14 15 16 17 18 19 struct SList(T) { 20 public { 21 Node!T* node = new Node!T; 22 } 23 public { 24 this(Node!T* node) { 25 this.node = node; 26 } 27 this(SList!T slist) { 28 this.node = slist.node; 29 } 30 } 31 public { 32 @property T value() { 33 assert(!empty, "No value, current node is a sentinel."); 34 return node.payload; 35 } 36 typeof(this.node) front() { 37 return this.node; 38 } 39 @property bool empty() { 40 import std.stdio; 41 "empty ".writeln(node.next); 42 return (node.next is null); 43 } 44 } 45 public { 46 SList!T dup() { 47 return SList!T(this); 48 } 49 void next() { 50 import std.stdio; 51 assert(!empty); 52 "next ".writeln(node.next); 53 node = node.next; 54 } 55 alias popFront = next; 56 } 57 public { 58 void removeNext() { 59 assert(!empty); 60 node.next = node.next.next; 61 } 62 void insertAfter(Node!T* newNode) { 63 newNode.next = node.next; 64 node.next = newNode; 65 } 66 void insertAfter(T value) { 67 insertAfter(new Node!T(null, value)); 68 } 69 70 void append(T value) { 71 assert(empty, "Can only append to the last node. If you want to force this operation use `redirect`."); 72 node.payload = value; 73 node.next = new Node!T; 74 } 75 76 void redirect(bool checkAtEnd=true)(Node!T* newNode) { 77 node.next = newNode; 78 } 79 void redirect(bool checkAtEnd=true)(SList!T slist) { 80 redirect!checkAtEnd(slist.node); 81 } 82 void redirect(bool checkAtEnd=true)(SListConsumable!T slist) { 83 redirect!checkAtEnd(slist.node); 84 } 85 } 86 } 87 88 89 90 unittest { 91 import std.stdio; 92 import std.algorithm.iteration; 93 import std.algorithm.searching; 94 95 SList!int s = SList!int(); 96 s.append(4); 97 98 foreach(a;s){ 99 a.value.writeln; 100 } 101 //// 102 s.insertAfter(5); 103 s.next; 104 s.next; 105 writeln; 106 foreach(a;s){a.value.writeln;} 107 ////s.find!(a=>a.empty).append(6); 108 109 writeln; 110 foreach(a;s){a.value.writeln;} 111 writeln; 112 foreach(a;s){a.value.writeln;} 113 114 s.each!writeln; 115 116 117 int[][] a = [[4,6,2],[1,2,3]]; 118 a.each!writeln; 119 } 120 121 122 123 124 125 126 127 128 129