当前位置:网站首页>Implementation of stack
Implementation of stack
2022-07-03 19:47:00 【Follow the road to the end】
We need to implement the following functions :
push() Stack method
pop() Stack method
top() Get stack top value
size() Get the number of elements of the stack
clear() Empty stack
class Stack {
constructor () {
// Store stack data
this.data = []
// Number of recorded data
this.count = 0
}
// Stack method
push (item) {
// this.data.push(item)
// this.data[this.data.length] = item
this.data[this.count] = item
this.count++
}
// Stack method
pop () {
if (this.isEmpty()) {
console.log(' The stack is empty. ')
return
}
const temp = this.data[this.count - 1]
delete this.data[--this.count]
return temp
}
// Check if the stack is empty
isEmpty () {
return this.count === 0
}
// Get stack top element
top () {
if (this.isEmpty()) {
console.log(' The stack is empty. ')
return
}
return this.data[this.count - 1]
}
// Get the number of elements
size () {
return this.count
}
// Empty stack
clear () {
this.data = []
this.count = 0
}
}
1
边栏推荐
- Leetcode daily question solution: 540 A single element in an ordered array
- Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro
- Typora charges, WTF? Still need support
- 5. MVVM model
- Microsoft: the 12th generation core processor needs to be upgraded to win11 to give full play to its maximum performance
- 5- (4-nitrophenyl) - 10,15,20-triphenylporphyrin ntpph2/ntppzn/ntppmn/ntppfe/ntppni/ntppcu/ntppcd/ntppco and other metal complexes
- About callback function and hook function
- Chapter 1: extend the same code decimal sum s (D, n)
- Chapter 1: seek common? Decimal and S (D, n)
- Zhang Fei hardware 90 day learning notes - personal record of day 3, please see my personal profile / homepage for the complete
猜你喜欢
2022-06-30 网工进阶(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
Sentinel source code analysis part II - sentinel dashboard console startup and configuration
Use of aggregate functions
QT -- qfileinfo file information reading
The necessity of lean production and management in sheet metal industry
2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)
2022 Xinjiang latest construction eight members (standard members) simulated examination questions and answers
02 -- QT OpenGL drawing triangle
Xctf attack and defense world crypto master advanced area olddriver
Wechat applet quick start (including NPM package use and mobx status management)
随机推荐
第一章:三位阶乘和数,图形点扫描
Pecan — Overview
Cross compile opencv with contrib
Day11 - my page, user information acquisition, modification and channel interface
Sentinel source code analysis part II - sentinel dashboard console startup and configuration
Leetcode daily question solution: 540 A single element in an ordered array
Class loading process
The space of C disk is insufficient, and the computer becomes stuck. Quickly expand the capacity of C disk to make the system more smooth
第一章:喝汽水,阶梯电费计算,阶梯电费计算函数,个人所税,求解平方根不等式,简化求解平方根不等式,求解调和级数不等式,解不等式:d<1+1/2-1/3+1/4+1/5-1/6+..士1/n
Blue Bridge Cup: the fourth preliminary - "simulated intelligent irrigation system"
NFT without IPFs and completely on the chain?
Summary of learning materials and notes of Zhang Fei's actual combat electronics 1-31
Chapter 1: recursively find the factorial n of n!
2022-06-28 advanced network engineering (XIII) IS-IS route filtering, route summary, authentication, factors affecting the establishment of Isis neighbor relations, other commands and characteristics
第二章:基于分解的求水仙花数,基于组合的求水仙花数, 兰德尔数,求[x,y]内的守形数,探求n位守形数,递推探索n位逐位整除数
PR 2021 quick start tutorial, how to create a new sequence and set parameters?
第二章:求长方体数组,指定区间内的完全数,改进指定区间内的完全数
Strict data sheet of new features of SQLite 3.37.0
6. Data agent object Defineproperty method
Wechat applet quick start (including NPM package use and mobx status management)