Created
April 29, 2014 15:59
-
-
Save subtubes-io/11404526 to your computer and use it in GitHub Desktop.
Abstract Data Type Data Structure (ADT) JavasScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function () { | |
| var myList; | |
| var List = function () { | |
| this.listSize = 0; | |
| this.pos = 0; | |
| this.dataStore = []; | |
| }; | |
| List.prototype.append = function (element) { | |
| this.dataStore[this.listSize] = element; | |
| this.listSize++; | |
| }; | |
| List.prototype.find = function (element) { | |
| var i; | |
| for (i = 0; i < this.listSize; i++) { | |
| if (this.dataStore[i] === element) { | |
| return i; | |
| } | |
| } | |
| return -1; | |
| }; | |
| List.prototype.remove = function (element) { | |
| var pos = this.find(element); | |
| if (pos > -1) { | |
| this.dataStore.splice(pos, 1); | |
| this.listSize--; | |
| return true; | |
| } | |
| return false; | |
| }; | |
| List.prototype.length = function () { | |
| return this.listSize; | |
| }; | |
| List.prototype.toString = function () { | |
| return this.dataStore; | |
| }; | |
| List.prototype.insert = function (after, element) { | |
| if (after > -1) { | |
| this.dataStore.splice(after, 0, element); | |
| this.listSize++; | |
| return true; | |
| } | |
| return false; | |
| }; | |
| List.prototype.clear = function () { | |
| delete this.dataStore; | |
| this.dataStore = []; | |
| this.listSize = 0; | |
| }; | |
| List.prototype.contains = function (element) { | |
| var i; | |
| for (i = 0; i < this.listSize; i++) { | |
| if (this.dataStore[i] === element) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| }; | |
| List.prototype.front = function () { | |
| this.pos = 0; | |
| }; | |
| List.prototype.end = function () { | |
| this.pos = this.listSize - 1; | |
| }; | |
| List.prototype.prev = function () { | |
| if (this.pos > 0) { | |
| this.pos--; | |
| } | |
| }; | |
| List.prototype.next = function () { | |
| if (this.pos < this.listSize - 1) { | |
| this.pos++; | |
| } | |
| }; | |
| List.prototype.currPos = function () { | |
| return this.pos; | |
| }; | |
| List.prototype.moveTo = function (pos) { | |
| this.pos = pos; | |
| }; | |
| List.prototype.getElement = function () { | |
| return this.dataStore[this.pos]; | |
| }; | |
| myList = new List(); | |
| myList.append("Hello World"); | |
| console.log(myList.find("Hello World")); | |
| }()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment