巧用javascript数组实现数据结构-堆栈与队列

简单的利用javascript的数组实现数据结构中的堆栈和队列。


Stack.js源码:

/*
 * @brief: 定义堆栈类
 * @remark: 实现堆栈基本功能
 */
function Stack(){
    //存储元素数组
    var aElement = new Array();
    /*
    * @brief: 元素入栈
    * @param: 入栈元素列表
    * @return: 堆栈元素个数
    * @remark: 1.Push方法参数可以多个
    *    2.参数为空时返回-1
    */
    Stack.prototype.Push = function(vElement){
        if (arguments.length == 0)
            return - 1;
        //元素入栈
        for (var i = 0; i < arguments.length; i++){
            aElement.push(arguments[i]);
        }
        return aElement.length;
    }
    /*
    * @brief: 元素出栈
    * @return: vElement
    * @remark: 当堆栈元素为空时,返回null
    */
    Stack.prototype.Pop = function(){
        if (aElement.length == 0)
            return null;
        else
            return aElement.pop();
    }
    /*
    * @brief: 获取堆栈元素个数
    * @return: 元素个数
    */
    Stack.prototype.GetSize = function(){
        return aElement.length;
    }
    /*
    * @brief: 返回栈顶元素值
    * @return: vElement
    * @remark: 若堆栈为空则返回null
    */
    Stack.prototype.GetTop = function(){
        if (aElement.length == 0)
            return null;
        else
            return aElement[aElement.length - 1];
    }
    /*
    * @brief: 将堆栈置空  
    */
    Stack.prototype.MakeEmpty = function(){
        aElement.length = 0;
    }
    /*
    * @brief: 判断堆栈是否为空
    * @return: 堆栈为空返回true,否则返回false
    */
    Stack.prototype.IsEmpty = function(){
        if (aElement.length == 0)
            return true;
        else
            return false;
    }
    /*
    * @brief: 将堆栈元素转化为字符串
    * @return: 堆栈元素字符串
    */
    Stack.prototype.toString = function(){
        var sResult = (aElement.reverse()).toString();
        aElement.reverse()
        return sResult;
    }
}

Queue.js源码:

/*
 * @brief: 定义队列类
 * @remark:实现队列基本功能
 */
function Queue(){
    //存储元素数组
    var aElement = new Array();
    /*
    * @brief: 元素入队
    * @param: vElement元素列表
    * @return: 返回当前队列元素个数
    * @remark: 1.EnQueue方法参数可以多个
    *    2.参数为空时返回-1
    */
    Queue.prototype.EnQueue = function(vElement){
        if (arguments.length == 0)
            return - 1;
        //元素入队
        for (var i = 0; i < arguments.length; i++){
            aElement.push(arguments[i]);
        }
        return aElement.length;
    }
    /*
    * @brief: 元素出队
    * @return: vElement
    * @remark: 当队列元素为空时,返回null
    */
    Queue.prototype.DeQueue = function(){
        if (aElement.length == 0)
            return null;
        else
            return aElement.shift();
 
    }
    /*
    * @brief: 获取队列元素个数
    * @return: 元素个数
    */
    Queue.prototype.GetSize = function(){
        return aElement.length;
    }
    /*
    * @brief: 返回队头素值
    * @return: vElement
    * @remark: 若队列为空则返回null
    */
    Queue.prototype.GetHead = function(){
        if (aElement.length == 0)
            return null;
        else
            return aElement[0];
    }
    /*
    * @brief: 返回队尾素值
    * @return: vElement
    * @remark: 若队列为空则返回null
    */
    Queue.prototype.GetEnd = function(){
        if (aElement.length == 0)
            return null;
        else
            return aElement[aElement.length - 1];
    }
    /*
    * @brief: 将队列置空  
    */
    Queue.prototype.MakeEmpty = function(){
        aElement.length = 0;
    }
    /*
    * @brief: 判断队列是否为空
    * @return: 队列为空返回true,否则返回false
    */
    Queue.prototype.IsEmpty = function(){
        if (aElement.length == 0)
            return true;
        else
            return false;
    }
    /*
    * @brief: 将队列元素转化为字符串
    * @return: 队列元素字符串
    */
    Queue.prototype.toString = function(){
        var sResult = (aElement.reverse()).toString();
        aElement.reverse()
        return sResult;
    }
}

测试:

var oStack = new Stack();
oStack.Push("abc", "123", 890);
console.log(oStack.toString());
oStack.Push("qq");
console.log(oStack.toString());
//  alert(oStack.GetSize());
//  alert(oStack.Pop());
//  alert(oStack.GetTop());
//  oStack.MakeEmpty();
//  alert(oStack.GetSize());
//  alert(oStack.toString());
delete oStack;
var oQueue = new Queue();
oQueue.EnQueue("bbs", "fans", "bruce");
console.log(oQueue.toString());
oQueue.EnQueue(23423);
console.log(oQueue.toString());
//  alert(oQueue.DeQueue());
//  alert(oQueue.GetSize());
//  alert(oQueue.GetHead());
//  alert(oQueue.GetEnd());
//  oQueue.MakeEmpty();
//  alert(oQueue.IsEmpty());
//  alert(oQueue.toString());
delete oQueue;


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值