当前位置:网站首页>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
边栏推荐
- Chapter 1: seek common? Decimal and S (D, n)
- Chapter 2: find the classical solution of the maximum Convention and the least common multiple of a and B, find the conventional solution of the maximum Convention and the least common multiple of a a
- Win10 share you don't have permission
- 第一章: 舍罕王失算
- Chapter 1: extend the same code decimal sum s (D, n)
- 01 - QT OpenGL display OpenGL window
- Part 27 supplement (27) buttons of QML basic elements
- Parental delegation mechanism
- [Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
- Chapter 1: find all factorial sums, Grand Prix site unified programming, three factorial sums, graphic point scanning, recursive factorial n of n!, Find the factorial n of n!, King Shehan miscalculate
猜你喜欢
Chapter 1: seek common? Decimal and S (D, n)
FPGA learning notes: vivado 2019.1 project creation
2022 - 06 - 30 networker Advanced (XIV) Routing Policy Matching Tool [ACL, IP prefix list] and policy tool [Filter Policy]
Acquisition and transmission of parameters in automatic testing of JMeter interface
Chapter 1: find all factorial sums, Grand Prix site unified programming, three factorial sums, graphic point scanning, recursive factorial n of n!, Find the factorial n of n!, King Shehan miscalculate
Today's work summary and plan: February 14, 2022
第一章:递归求n的阶乘n!
Merge K ascending linked lists
Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro
Chapter 1: recursively find the factorial n of n!
随机推荐
Typora charges, WTF? Still need support
Network security Kali penetration learning how to get started with web penetration how to scan based on nmap
2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)
Day10 ---- 强制登录, token刷新与jwt禁用
BOC protected tryptophan porphyrin compound (TAPP Trp BOC) Pink Solid 162.8mg supply - Qiyue supply
PR 2021 quick start tutorial, material import and management
Wechat applet quick start (including NPM package use and mobx status management)
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
Chapter 2: find the classical solution of the maximum Convention and the least common multiple of a and B, find the conventional solution of the maximum Convention and the least common multiple of a a
交叉编译Opencv带Contrib
Comments on flowable source code (37) asynchronous job processor
How to improve data security by renting servers in Hong Kong
WPF format datetime in TextBlock- WPF format DateTime in TextBlock?
2022-07-02 网工进阶(十五)路由策略-Route-Policy特性、策略路由(Policy-Based Routing)、MQC(模块化QoS命令行)
Phpstudy set LAN access
Chapter 1: King Shehan miscalculated
Rd file name conflict when extending a S4 method of some other package
Pecan - route
05 -- QT OpenGL draw cube uniform
Chapter 1: extend the same code decimal sum s (D, n)