巧用javascript數組實現資料結構-堆棧與隊列

來源:互聯網
上載者:User

簡單的利用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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.