JS数据结构--栈--常见操作
发布日期:2021-05-07 09:21:35 浏览次数:22 分类:技术文章

本文共 762 字,大约阅读时间需要 2 分钟。

一.栈的基本操作

栈可以通过存放数组链表来实现

1.通过数组实现

// 封装栈类function Stack(){   	// 栈中的属性	this.items = []		// 栈的相关操作	// 1.将元素压入栈	Stack.prototype.push() = function(element){   		this.items.push(element)	}	// 2.从栈中取出元素	Stack.prototype.pop() = function(){   		return this.items.pop()	}	// 3.查看一下栈顶元素	Stack.prototype.peek() = function(){   		return this.items[this.items.length - 1]	}	// 4.判断栈是否为空	Stack.prototype.isEmpty() = function(){   		return this.items.length == 0	}	// 5.获取栈中元素的个数	Stack.prototype.size() = function(){   		return this.items.length	}	// 6.toString方法	Stack.prototype.toString() = function(){   		var resultString = ''		for(var i = 0;i < this.items.length; i++){   			resultString += this.items[i] + ' '		}		return resultString	}}// 栈的使用var s = new Stack()s.push()
上一篇:【SSL】1033&【洛谷】P1040加分二叉树
下一篇:JS知识点--平时视频或做题时发现的知识漏洞(持续更新)

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月11日 19时20分14秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章