如何实现并使用一个支持长尾词的JavaScript栈类?
- 内容介绍
- 文章标签
- 相关推荐
本文共计417个文字,预计阅读时间需要2分钟。
本文实例讲述了JS栈(stack)类的实现与使用方法。分享给家长和广大读者,仅供参考。
栈是一种先进后出(FILO)的数据结构。原理如下:
+------------------+| 1 || 2 || 3 |+------------------+| 4 || 5 || 6 |+------------------+
示例代码:
javascriptfunction Stack() { this.items=[];
this.push=function(element) { this.items.push(element); };
this.pop=function() { return this.items.pop(); };
this.peek=function() { return this.items[this.items.length - 1]; };
this.isEmpty=function() { return this.items.length===0; };
this.size=function() { return this.items.length; };}
本文实例讲述了JS栈stack类的实现与使用方法。分享给大家供大家参考,具体如下:
栈是一种“先进后出”的数据结构,原理如下图所示:
示例代码:
/*使用栈stack类的实现*/ function stack() { this.dataStore = [];//保存栈内元素,初始化为一个空数组 this.top = 0;//栈顶位置,初始化为0 this.push = push;//入栈 this.pop = pop;//出栈 this.peek = peek;//查看栈顶元素 this.clear = clear;//清空栈 this.length = length;//栈内存放元素的个数 } function push(element){ this.dataStore[this.top++] = element; } function pop(){ return this.dataStore[--this.top]; } function peek(){ return this.dataStore[this.top-1]; } function clear(){ this.top = 0; } function length(){ return this.top; } /*测试stack类的实现*/ var s = new stack(); s.push("aa"); s.push("bb"); s.push("cc"); console.log(s.length());//3 console.log(s.peek());//cc var popped = s.pop(); console.log(popped);//cc console.log(s.peek());//bb
这里使用在线HTML/CSS/JavaScript代码运行工具:tools.jb51.net/code/HtmlJsRun测试上述代码,可得如下运行结果:
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript数据结构与算法技巧总结》、《JavaScript数学运算用法总结》、《JavaScript排序算法总结》、《JavaScript遍历算法与技巧总结》、《JavaScript查找算法技巧总结》及《JavaScript错误与调试技巧总结》
希望本文所述对大家JavaScript程序设计有所帮助。
本文共计417个文字,预计阅读时间需要2分钟。
本文实例讲述了JS栈(stack)类的实现与使用方法。分享给家长和广大读者,仅供参考。
栈是一种先进后出(FILO)的数据结构。原理如下:
+------------------+| 1 || 2 || 3 |+------------------+| 4 || 5 || 6 |+------------------+
示例代码:
javascriptfunction Stack() { this.items=[];
this.push=function(element) { this.items.push(element); };
this.pop=function() { return this.items.pop(); };
this.peek=function() { return this.items[this.items.length - 1]; };
this.isEmpty=function() { return this.items.length===0; };
this.size=function() { return this.items.length; };}
本文实例讲述了JS栈stack类的实现与使用方法。分享给大家供大家参考,具体如下:
栈是一种“先进后出”的数据结构,原理如下图所示:
示例代码:
/*使用栈stack类的实现*/ function stack() { this.dataStore = [];//保存栈内元素,初始化为一个空数组 this.top = 0;//栈顶位置,初始化为0 this.push = push;//入栈 this.pop = pop;//出栈 this.peek = peek;//查看栈顶元素 this.clear = clear;//清空栈 this.length = length;//栈内存放元素的个数 } function push(element){ this.dataStore[this.top++] = element; } function pop(){ return this.dataStore[--this.top]; } function peek(){ return this.dataStore[this.top-1]; } function clear(){ this.top = 0; } function length(){ return this.top; } /*测试stack类的实现*/ var s = new stack(); s.push("aa"); s.push("bb"); s.push("cc"); console.log(s.length());//3 console.log(s.peek());//cc var popped = s.pop(); console.log(popped);//cc console.log(s.peek());//bb
这里使用在线HTML/CSS/JavaScript代码运行工具:tools.jb51.net/code/HtmlJsRun测试上述代码,可得如下运行结果:
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript数据结构与算法技巧总结》、《JavaScript数学运算用法总结》、《JavaScript排序算法总结》、《JavaScript遍历算法与技巧总结》、《JavaScript查找算法技巧总结》及《JavaScript错误与调试技巧总结》
希望本文所述对大家JavaScript程序设计有所帮助。

