1 module LinkedListDemo; 2 3 import common; 4 5 import hunt.collection.AbstractList; 6 import hunt.collection.ArrayList; 7 import hunt.collection.LinkedList; 8 import hunt.collection.Collections; 9 import hunt.collection.List; 10 11 import std.stdio; 12 import std.conv; 13 import std.range; 14 15 // http://www.java2novice.com/java-collections-and-util/arraylist/ 16 class LinkedListDemo { 17 18 19 void testBasicOperations() { 20 LinkedList!(string) arrl = new LinkedList!(string)(); 21 //adding elements to the end 22 arrl.add("First"); 23 arrl.add("Second"); 24 arrl.add("Third"); 25 arrl.add("Random"); 26 27 assert(arrl.size == 4); 28 assert(arrl.getLast() == "Random"); 29 30 writeln("Actual LinkedList:" ~ arrl.toString()); 31 32 writeln("First Element: " ~ arrl.element()); 33 writeln("First Element: " ~ arrl.getFirst()); 34 writeln("First Element: " ~ arrl.peek()); 35 writeln("First Element: " ~ arrl.peekFirst()); 36 37 writeln("Last Element: " ~ arrl.getLast()); 38 writeln("Last Element: " ~ arrl.peekLast()); 39 40 List!(string) list = new ArrayList!(string)(); 41 list.add("one"); 42 list.add("two"); 43 arrl.addAll(list); 44 writeln("After Copy: " ~ arrl.toString()); 45 assert(arrl.size() == 6); 46 47 writeln("Adding element at last position..."); 48 arrl.addLast("I am last"); 49 writeln(arrl); 50 writeln("Adding element at last position..."); 51 arrl.offerLast("I am last - 1"); 52 writeln(arrl); 53 writeln("Adding element at last position..."); 54 arrl.offer("I am last - 2"); 55 assert(arrl.size() == 9); 56 writeln(arrl); 57 58 assert(arrl.toString() == "[First, Second, Third, Random, one, two, I am last, I am last - 1, I am last - 2]", 59 arrl.toString()); 60 61 // writeln("Actual LinkedList:"+arrl); 62 arrl.clear(); 63 assert(arrl.size() == 0); 64 writeln("After clear LinkedList:" ~ arrl.toString()); 65 66 } 67 68 void testRemove() { 69 LinkedList!(string) arrl = new LinkedList!(string)(); 70 arrl.add("First"); 71 arrl.add("Second"); 72 arrl.add("Third"); 73 arrl.add("Random"); 74 arrl.add("four"); 75 arrl.add("five"); 76 arrl.add("six"); 77 arrl.add("seven"); 78 arrl.add("eight"); 79 arrl.add("nine"); 80 writeln(arrl); 81 assert(arrl.size == 10); 82 83 writeln("\nRemov() method:" ~ arrl.remove()); 84 writeln("After remove() method call:"); 85 writeln(arrl); 86 assert(arrl.size == 9); 87 88 writeln("\nremove(index) method:" ~ arrl.removeAt(2).to!string()); 89 writeln("After remove(index) method call:"); 90 writeln(arrl); 91 assert(arrl.size == 8); 92 assert(arrl.toString() == "[Second, Third, four, five, six, seven, eight, nine]", 93 arrl.toString()); 94 95 writeln("\nRemov(object) method:" ~ arrl.remove("six").to!string()); 96 writeln("After remove(object) method call:"); 97 writeln(arrl); 98 assert(arrl.size == 7, arrl.size().to!string()); 99 100 writeln("\nremoveFirst() method:" ~ arrl.removeFirst()); 101 assert(arrl.size == 6, arrl.size().to!string()); 102 writeln("After removeFirst() method call:"); 103 writeln(arrl); 104 105 writeln("\nremoveFirstOccurrence() method:" ~ arrl.removeFirstOccurrence("eight")); 106 writeln("After removeFirstOccurrence() method call:"); 107 writeln(arrl); 108 109 writeln("\nremoveLast() method:" ~ arrl.removeLast()); 110 writeln("After removeLast() method call:"); 111 writeln(arrl); 112 assert(arrl.size == 4); 113 assert(arrl.toString() == "[Third, four, five, seven]", arrl.toString()); 114 115 writeln("\nremove a nonexisted member"); 116 assert(!(arrl.remove("First"))); 117 assert(arrl.size == 4); 118 119 // writeln("removeLastOccurrence() method:" ~ arrl.removeLastOccurrence("five")); 120 // writeln("After removeLastOccurrence() method call:"); 121 // writeln(arrl); 122 } 123 124 void testContains() { 125 LinkedList!(string) arrl = new LinkedList!(string)(); 126 //adding elements to the end 127 arrl.add("First"); 128 arrl.add("Second"); 129 arrl.add("Third"); 130 arrl.add("Random"); 131 writeln("Actual LinkedList:" ~ arrl.toString()); 132 133 List!(string) list = new LinkedList!(string)(); 134 list.add("Second"); 135 list.add("Random"); 136 writeln("Does LinkedList contains all list elements?: " ~ arrl.containsAll(list) 137 .to!string()); 138 list.add("one"); 139 writeln("Does LinkedList contains all list elements?: " ~ arrl.containsAll(list) 140 .to!string()); 141 } 142 143 void testPushPop() { 144 LinkedList!(string) arrl = new LinkedList!(string)(); 145 arrl.add("First"); 146 arrl.add("Second"); 147 arrl.add("Third"); 148 arrl.add("Random"); 149 writeln(arrl); 150 arrl.push("push element"); 151 writeln("After push operation:"); 152 writeln(arrl); 153 arrl.pop(); 154 writeln("After pop operation:"); 155 writeln(arrl); 156 } 157 158 // void testSubList() 159 // { 160 // LinkedList!(string) arrl = new LinkedList!(string)(); 161 // //adding elements to the end 162 // arrl.add("First"); 163 // arrl.add("Second"); 164 // arrl.add("Third"); 165 // arrl.add("Random"); 166 // arrl.add("Click"); 167 // writeln("Actual LinkedList:" ~ arrl.toString()); 168 // List!(string) list = arrl.subList(2, 4); 169 // writeln("Sub List: " ~ list.toString()); 170 // } 171 172 // void testReverse() 173 // { 174 // LinkedList!(string) list = new LinkedList!(string)(); 175 // list.add("Java"); 176 // list.add("Cric"); 177 // list.add("Play"); 178 // list.add("Watch"); 179 // list.add("Glass"); 180 // Collections.reverse(list); 181 // writeln("Results after reverse operation:"); 182 // foreach(string str; list){ 183 // writeln(str); 184 // } 185 // } 186 187 // void testShuffle() 188 // { 189 // LinkedList!(string) list = new LinkedList!(string)(); 190 // list.add("Java"); 191 // list.add("Cric"); 192 // list.add("Play"); 193 // list.add("Watch"); 194 // list.add("Glass"); 195 // list.add("Movie"); 196 // list.add("Girl"); 197 198 // Collections.shuffle(list); 199 // writeln("Results after shuffle operation:"); 200 // foreach(string str; list){ 201 // writeln(str); 202 // } 203 204 // Collections.shuffle(list); 205 // writeln("Results after shuffle operation:"); 206 // foreach(string str; list){ 207 // writeln(str); 208 // } 209 // } 210 211 // void testSwap() 212 // { 213 // LinkedList!(string) list = new LinkedList!(string)(); 214 // list.add("Java"); 215 // list.add("Cric"); 216 // list.add("Play"); 217 // list.add("Watch"); 218 // list.add("Glass"); 219 // list.add("Movie"); 220 // list.add("Girl"); 221 222 // Collections.swap(list, 2, 5); 223 // writeln("Results after swap operation:"); 224 // foreach(string str; list){ 225 // writeln(str); 226 // } 227 // } 228 229 }