@TOC
The method of stack
- Push (element) : Adds a new element to the top of the stack.
- Pop () : Removes an element from the top of the stack and returns the removed element.
- Peek () : returns the element at the top of the stack without making any changes to the stack (this method does not remove the element at the top of the stack, just returns it)
- IsEmpty () : returns true if there are no elements on the stack, false otherwise.
- Size () : returns the number of elements in the stack. This method is similar to the length property of an array.
- ToString () : Returns the contents of the stack structure as characters.
Description and definition of stack
A stack, also known as a stack, is a linear table with limited operations. A linear table that is restricted to insert and delete operations only at the end of the table. This end is called the top of the stack, and the other end is called the bottom of the stack. Inserting a new element into a stack, also known as a push, push, or push, is to put the new element on top of the top element of the stack, making it a new top element; Removing an element from a stack, also known as making a stack or making a stack, is to remove the top element of the stack so that its adjacent elements become the new top element.
A stack, “stack,” is a constrained linear table, last in first out.
The restriction is that you can only insert and delete on one end of the table. This end is called the top of the stack, and the other end is called the bottom of the stack. LIFO(last in first out) means the element that came in last, the first one that popped up in the stack space. Similar to an automatic meal tray, the tray placed last is often taken out first. Inserting a new element into a stack, also known as a push, push, or push, is to put the new element on top of the top element of the stack, making it a new top element; Removing an element from a stack, also known as making a stack or making a stack, is to remove the top element of the stack so that its adjacent elements become the new top element.
Simple encapsulated constructor -> stack
function Stack () {
// Stack properties
this.items = []
// 1. Push the element onto the stack
// this.push = function () {
Stack.prototype.push= function (element) {
this.items.push(element)
}
// 2. Remove elements from the stack
Stack.prototype.pop= function () {
return this.items.pop()
}
// 3. Look at the top of the stack
Stack.prototype.peek= function () {
return this.items[this.items.length - 1]}// 4. Check whether the stack is empty
Stack.prototype.isEmpty= function () {
return this.items.length === 0
}
// 5. Obtain the number of elements in the stack
Stack.prototype.size= function () {
return this.items.length
}
// 6. ToString method
Stack.prototype.toString= function () {
return this.items.join(' ')}}Copy the code
call
let$=new Stack()
$.push(20)
$.push(10)
$.push(100)
$.push(1008)
alert($)
$.pop()
$.pop()
alert($)
alert($.peek())
alert($.isEmpty())
alert($.size())
console.log($.toString())
console.log($)
Copy the code
Decimal to binary encapsulating function
// Convert decimal to binary
function dec2bin (decNumber) {
// 1. Define the object decNumber
let stack = new Stack()
// 2
while (decNumber > 0) {
// 2.1. Get the remainder and put it on the stack
stack.push(decNumber % 2)
// 2.2. Obtain the result of divisible division for the next operation
decNumber = Math.floor(decNumber / 2)}// 3. Remove 0 and 1 from stack
let binaryString = ' '
while(! stack.isEmpty()) { binaryString += stack.pop() }return binaryString
}
Copy the code