JavaScript Stack

function Stack() {
	var items = [];
	this.push = function(item) {
		items.push(item)
	}
	this.pop = function() {
		return items.pop()
	}
	this.peek = function() {
		return items[items.length - 1]
	}
	this.isEmpty = function() {
		return items.length == 0
	}
	this.size = function() {
		return items.length
	}
	this.clear = function() {
		items = []
	}
	this.printf = function() {
		console.log(items.toString())
	}
	this.divideBy2 = function(decNumber) {
		var remStack = new Stack(),
		rem,
		binaryString = ‘‘;
		while (decNumber > 0) {
			rem = Math.floor(decNumber % 2);
			remStack.push(rem);
			decNumber = Math.floor(decNumber / 2)
		}
		while (!remStack.isEmpty()) {
			binaryString += remStack.pop().toString()
		}
		return binaryString
	}
}
var stacks = new Stack();
console.log(stacks.isEmpty());
stacks.push(5);
stacks.push(4);
console.log(stacks.peek());
stacks.push(11);
console.log(stacks.size());
console.log(stacks.isEmpty());
stacks.push(15);
stacks.pop();
console.log(stacks.size());
stacks.printf();
console.log(stacks.divideBy2(33));

  

时间: 2024-10-07 03:20:55

JavaScript Stack的相关文章

JavaScript中null和undefined小结

一. 前言 因为对javascript中的null和undefined有些分不清楚,因此在试验之后进行一下小结. 二. 源码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title></title>

[it-ebooks]电子书列表

#### it-ebooks电子书质量不错,但搜索功能不是很好 #### 格式说明  [ ]中为年份      ||  前后是标题和副标题  #### [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Objective-C to develop iPhone games http://it-ebooks.info/book/3544/ Learning Web App Developmen

(转) [it-ebooks]电子书列表

[it-ebooks]电子书列表 [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Objective-C to develop iPhone games http://it-ebooks.info/book/3544/Learning Web App Development || Build Quickly with Proven JavaScript Techniques http://

摘录和再编:彻底弄懂JS执行机制

网文: https://juejin.im/post/59e85eebf265da430d571f89 并发模型和事件循环:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/EventLoop Node.js事件循环,Timers, process.nextTick() javascript是一门单线程语言,在最新的HTML5中提出了Web-Worker,但javascript是单线程这一核心仍未改变.所以一切javascript版的

Effective JavaScript Item 29 避免使用非规范的Stack Inspection属性

本系列作为Effective JavaScript的读书笔记. 由于历史原因,很多JavaScript执行环境中都提供了某些方式来查看函数调用栈.在一些环境中,arguments对象(关于该对象可以查看Item 22,23,24)上有两个额外的属性: arguments.callee - 它引用了正在被调用的函数 arguments.caller - 它引用了调用当前函数的函数 关于arguments.callee的使用,可以参考下面的代码: var factorial = (function(

JavaScript实现栈结构(Stack)

JavaScript实现栈结构(Stack) 一.前言 1.1.什么是数据结构? 数据结构就是在计算机中,存储和组织数据的方式. 例如:图书管理,怎样摆放图书才能既能放很多书,也方便取? 主要需要考虑两个问题: 操作一:新书怎么插入? 操作二:怎么找到某本指定的书? 常见的数据结构: 数组(Aarray) 栈(Stack) 链表(Linked List) 图(Graph) 散列表(Hash) 队列(Queue) 树(Tree) 堆(Heap) 注意:数据结构与算法与语言无关,常见的编程语言都有直

[LeetCode][JavaScript]Implement Stack using Queues

Implement Stack using Queues Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empt

[Javascript] Call Stack

Every time when a function run it will be push into the call stack and put on the top,  you can think call stack is something like a heap... Javascirpt has only one call stack. In the picture, main() get call first, so put into the call stack; second

使用javascript的stack数据结构,实现进制转换

function Stack() { var items = []; this.push = function(element){ items.push(element); } this.pop = function(){ return items.pop(); } this.peek = function() { return items[items.length -1]; } this.isEmpty = function() { return items.length == 0; } th