当前位置:网站首页>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: find the factorial n of n!
- 第一章:三位阶乘和数,图形点扫描
- 第一章:递归求n的阶乘n!
- Realize user registration and login
- Compared with 4G, what are the advantages of 5g to meet the technical requirements of industry 4.0
- Win10 share you don't have permission
- 2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程
- Read the paper glodyne global topology preserving dynamic network embedding
- 第二章:4位卡普雷卡数,搜索偶数位卡普雷卡数,搜索n位2段和平方数,m位不含0的巧妙平方数,指定数字组成没有重复数字的7位平方数,求指定区间内的勾股数组,求指定区间内的倒立勾股数组
- 第一章:求n的阶乘n!
猜你喜欢
Chapter 1: drinking soft drinks, step tariff calculation, step tariff calculation function, personal income tax, solving square root inequality, simplifying solving square root inequality, solving dem
HCIA-USG Security Policy
Make a simple text logo with DW
Geek Daily: the system of monitoring employees' turnover intention has been deeply convinced off the shelves; The meta universe app of wechat and QQ was actively removed from the shelves; IntelliJ pla
Chapter 2: find the box array, complete number in the specified interval, and improve the complete number in the specified interval
第一章:求n的阶乘n!
PR 2021 quick start tutorial, how to create new projects and basic settings of preferences?
2022.2.14 Li Kou - daily question - single element in an ordered array
BOC protected phenylalanine zinc porphyrin (Zn · TAPP Phe BOC) / iron porphyrin (Fe · TAPP Phe BOC) / nickel porphyrin (Ni · TAPP Phe BOC) / manganese porphyrin (Mn · TAPP Phe BOC) Qiyue Keke
10 smart contract developer tools that miss and lose
随机推荐
Common text labels
What is the content of game modeling
2022 - 06 - 30 networker Advanced (XIV) Routing Policy Matching Tool [ACL, IP prefix list] and policy tool [Filter Policy]
Redis master-slave synchronization, clustering, persistence
6. Data agent object Defineproperty method
Chapitre 1: le roi de shehan a mal calculé
Web Security (VIII) what is CSRF attack? Why can token prevent csdf attacks?
Point cloud data denoising
Floating source code comment (38) parallel job processor
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
FPGA 学习笔记:Vivado 2019.1 工程创建
Cross compile opencv with contrib
论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding
Titles can only be retrieved in PHP via curl - header only retrieval in PHP via curl
Phpstudy set LAN access
PR 2021 quick start tutorial, how to create new projects and basic settings of preferences?
Parental delegation mechanism
PR FAQ: how to set PR vertical screen sequence?
Leetcode 1189. Maximum number of balloons (special character count)
BOC protected tryptophan porphyrin compound (TAPP Trp BOC) Pink Solid 162.8mg supply - Qiyue supply