当前位置:网站首页>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
边栏推荐
- Compared with 4G, what are the advantages of 5g to meet the technical requirements of industry 4.0
- Professional interpretation | how to become an SQL developer
- Titles can only be retrieved in PHP via curl - header only retrieval in PHP via curl
- Sentinel source code analysis part II - sentinel dashboard console startup and configuration
- About unregistered transfer login page
- Bool blind note - score query
- WPF format datetime in TextBlock- WPF format DateTime in TextBlock?
- IPv6 experiment
- FPGA learning notes: vivado 2019.1 project creation
- Basic principle of LSM tree
猜你喜欢

05 -- QT OpenGL draw cube uniform

2022 Xinjiang latest road transportation safety officer simulation examination questions and answers

Chapter 20: y= sin (x) /x, rambling coordinate system calculation, y= sin (x) /x with profile graphics, Olympic rings, ball rolling and bouncing, water display, rectangular optimization cutting, R que

Wechat applet quick start (including NPM package use and mobx status management)

This Chinese numpy quick look-up table is too easy!

Make a simple text logo with DW

Kubernetes cluster builds efk log collection platform

2022-06-27 网工进阶(十二)IS-IS-开销类型、开销计算、LSP的处理机制、路由撤销、路由渗透

第二章:求长方体数组,指定区间内的完全数,改进指定区间内的完全数

2022-07-02 网工进阶(十五)路由策略-Route-Policy特性、策略路由(Policy-Based Routing)、MQC(模块化QoS命令行)
随机推荐
Native table - scroll - merge function
03 -- QT OpenGL EBO draw triangle
Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
Sentinel source code analysis part II - sentinel dashboard console startup and configuration
Microsoft: the 12th generation core processor needs to be upgraded to win11 to give full play to its maximum performance
Acquisition and transmission of parameters in automatic testing of JMeter interface
2022.2.14 Li Kou - daily question - single element in an ordered array
Cross compile opencv with contrib
Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!
Kubernetes cluster builds efk log collection platform
Chapter 2: 4-digit Kaplan number, search even digit Kaplan number, search n-digit 2-segment sum square number, m-digit ingenious square number without 0, specify the number to form a 7-digit square nu
2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)
What is the content of game modeling
Day11 - my page, user information acquisition, modification and channel interface
Leetcode daily question solution: 540 A single element in an ordered array
IPv6 experiment
QT -- qfileinfo file information reading
Bright purple crystal meso tetra (4-aminophenyl) porphyrin tapp/tapppt/tappco/tappcd/tappzn/tapppd/tappcu/tappni/tappfe/tappmn metal complex - supplied by Qiyue
Professional interpretation | how to become an SQL developer
Bool blind note - score query