adds a new node at the end of the list
adds new nodes at end of list from an array
clears/resets the list. Frees all the occupied memory, & removes all items
Removes all bookmarks
Returns true if list contains a node, i.e searches for a node and returns true if found
Returns true if list contains all elements provided in an array, else, false
Inserts a node after the position of last-read-node To insert at beginning, call resetRead before inserting
Inserts an array of nodes after the position of last-read-node If there is no last-read-item, the item is inserted at beginning. To do this, call resetRead before inserting Returns true on success, false on failure
moves read/insert position back to a bookmark using the bookmark ID Does NOT delete the bookmark. Use LinkedList.removeBookmark to delete Retutns true if successful false if the bookmark ID no longer exists
Sets a "bookmark", and returns the bookmark-ID, throws Exception if there is no last-read-item to place bookmark on
returns pointer of next node to be read, null if there are no more nodes
Returns the pointer to the first node in the list
Returns the pointer to the last node in the list
removes a bookmark using the bookmark id returns true if successful false if the bookmark doesn't exist
removes the first node in list
removes the node that was last read using LinkedList.read. The last node cannot be removed using this. returns true on success
resets the read position, i.e: set reading position to first node, and nulls the last-read-ptr
Reads the list into an array, and returns the array
number of items that the list is holding
Unittests for utils.lists.LinkedList
1 LinkedList!ubyte list = new LinkedList!ubyte; 2 //`LinkedList.append` and `LinkedList.read` and `LinkedList.readFirst` and `LinkedList.readLast` and `LinkedList.resetRead` 3 list.append(0); 4 list.append(1); 5 list.append(2); 6 assert(*(list.readFirst()) == 0); 7 assert(*(list.readLast()) == 2); 8 assert(list.count == 3); 9 list.read();// to skip, we wanna read the node at index 1 (2nd node) 10 assert(*(list.read()) == 1); 11 list.resetRead(); 12 assert(*(list.read()) == 0); 13 // `LinkedList.append(T[])`: 14 list.clear(); 15 list.append(0); 16 list.append([1, 2, 3]); 17 assert(list.count == 4); 18 assert(list.toArray ==[0, 1, 2, 3]); 19 list.clear; 20 list.append([0, 1, 2]); 21 list.append(3); 22 assert(list.count == 4); 23 assert(list.toArray == [0, 1, 2, 3]); 24 //`LinkedList.clear` 25 list.clear(); 26 list.append(3); 27 list.append(4); 28 assert(*(list.read()) == 3); 29 assert(list.count == 2); 30 list.clear(); 31 //`LinkedList.removeLastRead` and `Linkedlist.removeFirst` 32 list.append(0); 33 list.append(1); 34 list.append(2); 35 list.read(); 36 list.read(); 37 list.removeLastRead(); 38 list.resetRead(); 39 assert(*(list.read()) == 0); 40 assert(*(list.read()) == 2); 41 assert(list.count == 2); 42 list.removeFirst(); 43 list.resetRead(); 44 assert(*(list.read()) == 2); 45 assert(list.count == 1); 46 list.removeLastRead(); 47 assert(list.count == 0); 48 //`LinkedList.toArray` and `LinkedList.insertNode` and `LinkedList.insertNodes` 49 list.clear();// to reset stuff 50 list.append(0); 51 list.append(4); 52 list.read(); 53 list.insert(1); 54 assert(*(list.read()) == 1); 55 list.insert([2, 3]); 56 list.resetRead(); 57 assert(list.count == 5); 58 assert(list.toArray == [0, 1, 2, 3, 4]); 59 //`Linkedlist.hasElement` and `LinkedList.hasElements` 60 assert(list.hasElement(0) == true); 61 assert(list.hasElement(4) == true); 62 assert(list.hasElement(5) == false); 63 assert(list.hasElement(7) == false); 64 assert(list.hasElements([3, 1, 2, 0, 4]) == true); 65 assert(list.hasElements([0, 1, 2, 6]) == false); 66 // `LinkedList.insert` at beginning 67 list.clear; 68 list.insert([1, 2]); 69 list.insert(0); 70 assert(list.count == 3); 71 assert(list.toArray == [0, 1, 2]); 72 //destroying last item 73 list.clear(); 74 list.append(0); 75 list.append(1); 76 list.append(2); 77 list.read(); 78 list.read(); 79 list.read(); 80 assert(list.removeLastRead() == true); 81 assert(list.toArray() == [0, 1]); 82 //bookmarks 83 list.clear; 84 list.append([0, 1, 2, 3, 4, 5]); 85 assert(*list.read == 0); 86 assert(*list.read == 1); 87 assert(*list.read == 2); 88 { 89 uinteger id = list.placeBookmark; 90 assert(*list.read == 3); 91 assert(list.moveToBookmark(id + 1) == false); 92 assert(list.moveToBookmark(id) == true); 93 assert(*list.read == 3); 94 assert(list.removeBookmark(id) == true); 95 } 96 destroy(list);
A linked list, used where only reading in the forward direction is required