JavaScript 列表和栈的实现

列表类的实现

1. 定义构造函数
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.hasNext;
        this.hasPrev;
        this.length = length;
        this.currPos = currPos;
        this.moveTo = moveTo;
        this.getElement = getElement;
        this.contains = contains;
    }
2.实现方法
append:给列表添加元素

该方法给列表的下一个位置增加一个新的元素,这个位置刚好等于变量listSize的值

 function append(element){
        this.dataStore[this.listSize++] = element;
    }
find:在列表中查找某一元素

该方法通过对数组对象dataStore进行迭代,查找给定的元素。如果找到,就返回该元素在列表中的位置,否则返回-1

function find(element){
        for (var i=0;i<this.dataStore.length;i++){
            if (this.dataStore[i] == element){
                return i;
            }
        }
        return -1;
    }
remove:从列表中删除元素

首先,需要在列表中找到该元素,然后删除它,并且调整底层的数组对象以填补删除该元素后留下的空白。

function remove(element) {
        var foundAt = this.find(element);
        if (foundAt > -1){
            this.dataStore.splice(foundAt,1);
            --this.listSize;
            return true;
        }
        return false;
    }
length:列表中有多少个元素

该方法返回列表中元素的个数

 function length() {
        return this.listSize;
    }
toString:显示列表中的元素
    function toString() {
        return this.dataStore;
    }
insert:向列表中插入一个元素

该方法会寻找插入的after参数在列表中的位置,找到该位置后,使用splice()方法将新元素插入该位置之后,然后将变量listSize加1并返回true,表明插入成功。

 function insert(element, after) {
        var insertPos = this.find(after);
        if (insertPos > -1){
            this.dataStore.splice(insertPos+1,0,element);
            ++this.listSize;
            return true;
        }
        return false;
    }
clear:清空列表中的所有元素

该方法使用delete操作符删除数组dataStore,接着在下一行创建一个空数组。最后将listSize和pos的值设为1

function clear() {
        delete this.dataStore;
        this.dataStore.length = 0;
        this.listSize = this.pos = 0;
    }
contains:判断给定值是否在列表中
function contains(element) {
        for (var i=0;i<this.dataStore.length;i++){
            if (this.dataStore[i] == element){
                return true;
            }
        }
        return false;
    }
遍历列表

这组方法允许用户在列表上自由移动

/*
    将列表的当前位置移动到第一个元素
     */
    function front() {
        this.pos = 0;
    }

    /*
    将列表的当前位置移动到最后一个元素
     */
    function end() {
        this.pos = this.listSize - 1;
    }

    /*
    将当前位置后移一位
     */
    function prev() {
        --this.pos;
    }

    /*
    将当前位置前移一位
     */
    function next() {
        if (this.pos < this.listSize){
            ++this.pos;
        }
    }

    /*
    返回列表当前位置
     */
    function currPos() {
        return this.pos;
    }

    /*
    将当前位置移动到指定位置
     */
    function moveTo(position) {
        this.pos = position;
    }

    /*
    返回当前位置的元素
     */
    function getElement() {
        return this.dataStore[this.pos];
    }

    /*
    判断后一位
     */
    function hashNext() {
        return this.pos<this.listSize;
    }

    /*
    判断前一位
     */
    function hasPrev() {
        return this.pos >= 0;
    }

栈的实现

1.定义构造函数
 function Stack() {
        this.dataStore = [];
        this.top = 0;
        this.push = push;
        this.pop = pop;
        this.peek = peek;
        this.clear = clear;
        this.length = length;
    }
2.实现方法
push()方法

当向栈中压入一个新元素时,需要将其保存在数组中变量top所对应的位置,然后将top值加1,让其指向数组中下一个空位置。

function push(element) {
        this.dataStore[this.top++] = element;
    }
pop()方法

pop()方法与push()方法相反,它返回栈顶元素,同时将变量top的值减1。

function pop() {
        return this.dataStore[--this.top];
    }
peek()方法

该方法返回数组的第top-1个位置的元素,即栈顶元素。

    function peek() {
        return this.dataStore[this.top-1];
    }
length()方法

该方法通过返回变量top值的方式返回栈内的元素个数

    function length() {
        return this.top;
    }
clear()方法

将top值设为0,清空栈

    function clear() {
        this.top = 0;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值