数据结构----列表

//算法-----列表

/*
* 列表是一组有序的数据。每个列表中的数据项称为元素。在 JavaScript 中,列表中的元素
 可以是任意数据类型。列表中可以保存多少元素并没有事先限定,实际使用时元素的数量
 受到程序内存的限制

 列表的抽象数据类型定义:

 listSize(属性) 列表的元素个数 作为内部属性
 pos(属性) 列表的当前位置
 dataStore: 初始化一个空数组保存列表元素
 length(方法) 返回列表中元素的个数
 clear(方法) 清空列表中的所有元素
 toString(方法) 返回列表的字符串形式
 getElement(方法) 返回当前位置的元素
 insert(方法) 在现有元素后插入新元素
 append(方法) 在列表的末尾添加新元素
 remove(方法) 从列表中删除元素
 front(方法) 将列表的当前位置设移动到第一end(方法) 将列表的当前位置移动到最后一prev(方法) 将当前位置后移一位
 next(方法) 将当前位置前移一位
 currPos(方法) 返回列表的当前位置
 moveTo(方法) 将当前位置移动到指定位置
* */


//实现列表数据结构

function append(element) {
    this.dataStore[this.listSize++] = element;
}
function find(element) {
    for(var i = 0; i< this.dataStore.length;i++) {
        if (element === this.dataStore[i]) {
            return i;
        }
    }
    return -1;
}
function remove(element) {
    var index = this.find(element);
    if (index > -1) {
        this.dataStore.splice(index,1);
        --this.listSize;
        return true;
    }
    return false;
}
function length() {
    return this.listSize;
}
function toString() {
    return this.dataStore.toString();
}
function insert(element,after) {
    var index = this.find(after);
    if (index > -1) {
        this.dataStore.splice(++index,0,element);
        this.listSize++;
        return true;
    }
    return false;
}
function clear() {
    delete this.dataStore;
    this.dataStore = [];
    this.listSize = 0;
    this.pos = 0;
}
function contains(element) {
    for(var i = 0; i< this.dataStore.length;i++) {
        if (element === this.dataStore[i]) {
            return true;
        }
    }
    return false;
}
function front() {
    this.pos = 0;
}
function end() {
    this.pos = this.dataStore.length -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];
}


function List() {
    this.listSize = 0;
    this.pos = 0;
    this.dataStore = [];
    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.contains = contains;
}

function createMovies(url) {
    var arr =  fs.readFileSync(url,"utf-8").split('
'),
        len = arr.length;
    for(var i= 0;i< len;i++){
        arr[i] = arr[i].trim();
    }
    return arr;
}


var Customer = function (name,movie) {
    this.name = name;
    this.movie = movie;
};

//借出影碟
/**
   * 用户拿走影碟
   * @param name 用户的名字
   * @param movie 影碟的名字
   * @param movieList 所有影碟列表
  * @param customerList 用户列表
 */
function checkOut(name,movie,movieList,customerList) {
    if (movieList.find(movie) > -1){
        var user = new Customer(name,movie);
        customerList.append(user);
        movieList.remove(movie);
    }else {
        console.log("没有该电影")
    }
}

function displayList(list) {
    for(list.front();list.currPos() < list.length();list.next()) {
        console.log(list.getElement())
    }
}

var fs = require("fs");
var ms = createMovies("./my.txt");
var movieList = new List();  //电影列表
var customers = new List(); //用户列表
for(var i = 0 ; i < ms.length;i++){
    movieList.append(ms[i]);
}

checkOut("Jane","教父",movieList,customers);

displayList(customers);

console.log("========");

displayList(movieList);

// names.append("Clayton");
// names.append("Raymond");
// names.append("Cynthia");
// names.append("Jennifer");
// names.append("Bryan");
// names.append("Danny");
//
// console.log(names.toString());
//
// names.front();
//
// console.log(names.getElement());
//
// names.next();
//
// console.log(names.getElement())
原文地址:https://www.cnblogs.com/yunnex-xw/p/9818120.html