Created
June 8, 2014 19:54
-
-
Save theodesp/9c22863050afdc59aa2b to your computer and use it in GitHub Desktop.
List Data Structure
This file contains 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 List() { | |
this.listSize = 0; | |
this.pos = 0; | |
this.dataStore = []; // initializes an empty array to store list elements | |
this.clear = clear; | |
this.find = find; | |
this.toString = toString; | |
this.insert = insert; | |
this.append = append; | |
this.remove = remove; | |
this.start = start; | |
this.end = end; | |
this.prev = prev; | |
this.next = next; | |
this.length = length; | |
this.currPos = currPos; | |
this.at = at; | |
this.getElement = getElement; | |
this.length = length; | |
this.contains = contains; | |
} | |
// Push to the end | |
function push(element) { | |
this.dataStore[this.listSize++] = element; // O(1) | |
} | |
// Find element in array | |
function find(element) { | |
for (var i = 0; i < this.dataStore.length; ++i) { // O(n) | |
if (this.dataStore[i] == element) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
// Remove element if exists | |
function remove(element) { | |
var foundAt = this.find(element); // O(n) | |
if (foundAt > -1) { | |
this.dataStore.splice(foundAt, 1); // O(n) | |
--this.listSize; | |
return true; | |
} | |
return false; | |
} | |
// Get length | |
function length() { | |
return this.listSize; // O(1) | |
} | |
// Print the array | |
function toString() { | |
return this.dataStore; // O(n) | |
} | |
// Insert the element after | |
function insert(element, after) { | |
var insertPos = this.find(after); // O(n) | |
if (insertPos > -1) { | |
this.dataStore.splice(insertPos + 1, 0, element); // O(n) | |
++this.listSize; | |
return true; | |
} | |
return false; | |
} | |
// Zero the list | |
function clear() { // O(1) | |
delete this.dataStore; | |
this.dataStore = []; | |
this.listSize = this.pos = 0; | |
} | |
// Check if element exists in the list | |
function contains(element) { | |
for (var i = 0; i < this.dataStore.length; ++i) { // O(n) | |
if (this.dataStore[i] == element) { | |
return true; | |
} | |
} | |
return false; | |
} | |
// First | |
function start() { | |
this.pos = 0; // O(1) | |
} | |
// last | |
function end() { | |
this.pos = this.listSize - 1; // O(1) | |
} | |
// Previus | |
function prev() { | |
if (this.pos > 0) { // O(1) | |
--this.pos; | |
} | |
} | |
// Next | |
function next() { | |
if (this.pos < this.listSize - 1) { // O(1) | |
++this.pos; | |
} | |
} | |
// Current | |
function currPos() { | |
return this.pos; // O(1) | |
} | |
// Go at position | |
function at(position) { | |
this.pos = position; // O(1) | |
} | |
// Get current element | |
function getElement() { | |
return this.dataStore[this.pos]; // O(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment