|
| 1 | +function List() { |
| 2 | + this.listSize = 0; // number of elements |
| 3 | + this.pos = 0; // current position in list |
| 4 | + this.length = length; // returns the number of elements in list |
| 5 | + this.clear = clear; // clears all elements from list |
| 6 | + this.toString = toString; // returns string representation of list |
| 7 | + this.getElement = getElement; // returns element at current position |
| 8 | + this.insert = insert; // Inserts new element to the end of list |
| 9 | + this.append = append; // adds new element to end of list |
| 10 | + this.remove = remove; // removes element from list |
| 11 | + this.front = front; // sets current position to first element list |
| 12 | + this.end = end; // sets current position to last element of list |
| 13 | + this.prev = prev; // moves current position to last element of list |
| 14 | + this.next = next; // moves current position forward one element |
| 15 | + this.currPos = currPos; // returns the current position in list |
| 16 | + this.moveTo = moveTo; // moves the current position to specified position |
| 17 | + this.dataStore = []; |
| 18 | + this.find = find; |
| 19 | + this.contains = contains; |
| 20 | +} |
| 21 | + |
| 22 | +// adds new element to end of list |
| 23 | +function append(element) { |
| 24 | + this.dataStore[this.listSize++] = element; |
| 25 | +} |
| 26 | + |
| 27 | +function find(element) { |
| 28 | + for (var i = 0; i < this.dataStore.length; ++i) { |
| 29 | + if (this.dataStore[i] == element) { |
| 30 | + return i; |
| 31 | + } |
| 32 | + } |
| 33 | + return -1; |
| 34 | +} |
| 35 | + |
| 36 | +// removes element from list |
| 37 | +function remove(element) { |
| 38 | + var foundAt = this.find(element); |
| 39 | + if (foundAt > -1) { |
| 40 | + this.dataStore.splice(foundAt, 1); |
| 41 | + --this.listSize; |
| 42 | + return true; |
| 43 | + } |
| 44 | + return false; |
| 45 | +} |
| 46 | + |
| 47 | +// returns the number of elements in list |
| 48 | +function length() { |
| 49 | + return this.listSize; |
| 50 | +} |
| 51 | + |
| 52 | +// returns string representation of list |
| 53 | +function toString() { |
| 54 | + return this.dataStore; |
| 55 | +} |
| 56 | + |
| 57 | +// Inserts new element to the end of list |
| 58 | +function insert(element, after) { |
| 59 | + var insertPos = this.find(after); |
| 60 | + if (insertPos > -1) { |
| 61 | + this.dataStore.splice(insertPos + 1, 0, element); |
| 62 | + ++this.listSize; |
| 63 | + return true; |
| 64 | + } |
| 65 | + return false; |
| 66 | +} |
| 67 | + |
| 68 | +// clears all elements from list |
| 69 | +function clear() { |
| 70 | + delete this.dataStore; |
| 71 | + this.dataStore = []; |
| 72 | + this.listSize = this.pos = 0; |
| 73 | +} |
| 74 | + |
| 75 | +function contains(element) { |
| 76 | + for (var i = 0; i < this.dataStore.length; ++i) { |
| 77 | + if (this.dataStore[i] === element) { |
| 78 | + return true; |
| 79 | + } |
| 80 | + } |
| 81 | + return false; |
| 82 | +} |
| 83 | + |
| 84 | +// sets current position to first element list |
| 85 | +function front() { |
| 86 | + this.pos = 0; |
| 87 | +} |
| 88 | + |
| 89 | +// sets current position to last element of list |
| 90 | +function end() { |
| 91 | + this.pos = this.listSize - 1; |
| 92 | +} |
| 93 | + |
| 94 | +// moves current position to last element of list |
| 95 | +function prev() { |
| 96 | + if (this.pos > 0) { |
| 97 | + --this.pos; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// moves current position forward one element |
| 102 | +function next() { |
| 103 | + if (this.pos < this.listSize - 1) { |
| 104 | + ++this.pos; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// returns the current position in list |
| 109 | +function currPos() { |
| 110 | + return this.pos; |
| 111 | +} |
| 112 | + |
| 113 | +// moves the current position to specified position |
| 114 | +function moveTo(position) { |
| 115 | + this.pos = position; |
| 116 | +} |
| 117 | + |
| 118 | +// returns element at current position |
| 119 | +function getElement() { |
| 120 | + return this.dataStore[this.pos]; |
| 121 | +} |
| 122 | + |
| 123 | +var names = new List(); |
| 124 | + |
| 125 | +names.append('Clayton'); |
| 126 | +names.append('Raymond'); |
| 127 | +names.append('Cynthia'); |
| 128 | +names.append('Jennifer'); |
| 129 | +names.append('Bryan'); |
| 130 | +names.append('Danny'); |
| 131 | + |
| 132 | +names.front(); |
| 133 | + |
| 134 | +console.log(names.toString(), 'toString'); |
| 135 | + |
| 136 | +names.remove('Raymond'); |
| 137 | + |
| 138 | +names.next(); |
| 139 | + |
| 140 | +console.log(names.getElement(), 'getElement'); |
| 141 | + |
| 142 | +names.next(); |
| 143 | +names.next(); |
| 144 | +names.prev(); |
| 145 | + |
| 146 | +console.log(names.getElement(), 'getElement'); |
| 147 | + |
| 148 | +console.log(names.toString(), 'toString'); |
0 commit comments