Skip to content

Instantly share code, notes, and snippets.

@jmingov
Created July 28, 2015 23:45
Show Gist options
  • Save jmingov/193ec7d08fdb10690956 to your computer and use it in GitHub Desktop.
Save jmingov/193ec7d08fdb10690956 to your computer and use it in GitHub Desktop.
js list management
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.front = front;
this.end = end;
this.prev = prev;
this.next = next;
this.length = length;
this.currPos = currPos;
this.moveTo = moveTo;
this.getElement = getElement;
this.length = length;
this.contains = contains;
}
/*
Append: Adding an Element to a List
*/
/*
This function appends a
new element onto the list at the next available position, which will be equal to the value
of the listSize variable
*/
function append(element) {
this.dataStore[this.listSize++] = element; // After the element is appended, listSize is incremented by 1.
}
/*
Remove: Removing an Element from a List
*/
/*
Next, let’s see how to remove an element from a list. remove() is one of the harder
functions to implement in the List class. First, we have to find the element in the list,
and then we have to remove it and adjust the space in the underlying array to fill the
hole left by removing an element. However, we can simplify the process by using the
splice() mutator function. To start, let’s define a helper function, find(), for finding
the element to remove
*/
function find(element) { // The find function simply iterates through dataStore looking for the specified element.
for (var i = 0; i < this.dataStore.length; ++i) {
if (this.dataStore[i] == element) {
return i; // If the element is found, the function returns the position where the element was found.
}
}
return -1; // If the element wasn’t found, the function returns -1, which is a standard value to return when an element can’t be found in an array.
}
/*
Find: Finding an Element in a List
*/
function remove(element) {
var foundAt = this.find(element);
if (foundAt > -1) {
this.dataStore.splice(foundAt,1); // https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
--this.listSize;
return true; // The function returns true if an element is removed
}
return false; // and false otherwise
}
/*
Length: Determining the Number of Elements in a List
*/
function length() { // The length() function returns the number of elements in a list
return this.listSize;
}
/*
toString: Retrieving a List’s Elements
*/
function toString() { // function that allows us to view the elements of a list
return this.dataStore;
}
/*
Insert: Inserting an Element into a List
*/
function insert(element, after) {
var insertPos = this.find(after); // insert() uses the helper function find() to determine the correct insertion position
if (insertPos > -1) {
this.dataStore.splice(insertPos+1, 0, element); // Once this position is found, we use splice() to insert the new element into the list.
++this.listSize; // Then we increment listSize by 1 and return true to indicate the insertion was successful
return true;
}
return false;
}
/*
Clear: Removing All Elements from a List
*/
function clear() { // function to clear out the elements of a list and allow new elements to be entered
delete this.dataStore; // uses the delete operator to delete the dataStore array,
this.dataStore = []; // and the next line re-creates the empty array
this.listSize = this.pos = 0; // ets the values of listSize and pos to 0 to indicate the start of a new list
}
/*
Contains: Determining if a Given Value Is in a List
*/
function contains(element) { // want to check a list to see if a particular value is part of the list
for (var i = 0; i < this.dataStore.length; ++i) {
if (this.dataStore[i] == element) {
return true;
}
}
return false;
}
/*
Traversing a List
*/
// This final set of functions allows movement through a list, and the last function, getElement(), displays the current element in a list
function front() {
this.pos = 0;
}
function end() {
this.pos = this.listSize-1;
}
function prev() {
if (this.pos > 0) {
--this.pos;
}
}
function next() {
if (this.pos < this.listSize-1) {
++this.pos;
}
}
function currPos() {
return this.pos;
}
function moveTo(position) {
this.pos = position;
}
function getElement() {
return this.dataStore[this.pos];
}
/*
Iterating Through a List
*/
// for(LIST.front(); LIST.currPos() < LIST.length(); LIST.next()) { // With these advantages in mind, here is how to use an iterator to traverse through a list.
// print(LIST.getElement());
// }
// for(LIST.end(); LIST.currPos() >= 0; LIST.prev()) { // We can also traverse a list backward using an iterator.
// print(LIST.getElement());
// }
/*
A List-Based Application
*/
function createArr(file) { // code fragment to read the contents of the file into our program
var arr = read(file).split("\n");
for (var i = 0; i < arr.length; ++i) {
arr[i] = arr[i].trim();
}
return arr;
}
/*
Using Lists to Manage a Kiosk
*/
var movieList = new List(); // take the movies array and store its contents in a list
for (var i = 0; i < movies.length; ++i) {
movieList.append(movies[i]);
}
function displayList(list) { // write a function to display the movie list available at the kiosk
for (list.front(); list.currPos() < list.length(); list.next()) {
if (list.getElement() instanceof Customer) { // For each object in the list, we use the instanceof operator to test whether the object is a Customer object.
print(list.getElement()["name"] + ", " + list.getElement()["movie"]);
}
else {
print(list.getElement()); // If the object is not a Customer, the code simply returns the element.
}
}
}
var customers = new List();
function Customer(name, movie) { // constructor function for the Customer object
this.name = name;
this.movie = movie;
}
// we need a function that allows a customer to check out a movie. This function
// takes two arguments: the customer’s name and the movie he wants to check out. If the
// movie is available, the function removes the movie from the kiosk’s list of movies and
// adds it to the customer’s list.
function checkOut(name, movie, filmList, customerList) { // Here is the definition for a function to check out a movie
if (movieList.contains(movie)) { // If the movie is available,
var c = new Customer(name, movie); // a Customer object is created with the movie’s title and the customer’s name.
customerList.append(c); // The Customer object is appended to the customer list,
filmList.remove(movie); // and the movie is removed from the movie list.
}
else {
print(movie + " is not available."); // If the movie is not available
}
}
// We can test the checkOut() function with a short program
var movies = createArr("films.txt");
var movieList = new List();
var customers = new List();
for (var i = 0; i < movies.length; ++i) {
movieList.append(movies[i]);
}
print("Available movies: \n");
displayList(movieList);
checkOut("Jane Doe", "The Godfather", movieList, customers);
print("\nCustomer Rentals: \n");
displayList(customers);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment