: (stack) also known as stack, it is a linear table with limited operations. Follow the last-in-first-out (LIFO)
top of the stack : a linear list that restricts insertion and deletion operations only to the end of the table, and
the bottom of the stack : a linear list that restricts insertion and deletion operations only to the head.
Pushing : Inserting a new element into a stack is also called pushing, pushing or pushing. It is to put the new element on top of the top element of the stack, making it the new top element of the stack;
popping : deleting from a stack Element is also called popping or popping off the stack. It deletes the top element of the stack and makes its adjacent elements become the new top element of the stack.
what process-oriented is:
Process-oriented is to analyze the steps to solve the problem and
then use functions
to implement them. You only need to execute and call them step by step.
- push (element ) adds one or more elements to the top of the stack
- pop () deletes the top element and returns the removed element
- peek () returns the element on the top of the stack
- isEmpty () is used to determine whether the stack is If empty, it will be empty.
- clear() is used to clear the elements of the stack.
- size() is used to return the number of elements in the stack.
Before implementing it, let’s think about how we implement it
. First, we borrow the array method to implement it, so we need to create
an Empty array to simulate stack
to build a class, use an array to simulate it, and
write various methods in the class
to partially call the array method.
In general, the method of using a class to wrap
an array is to realize the simulation of the stack
class Stack { constructor() { this.item = [] } push(element) { this.item.push(element) } pop() { return this.item.pop() } peek() { return this.item[this.item.length - 1] } isEmpty() { return this.item.length === 0 } clear() { this.item = [] size() { return this.item.length } } //Instantiate the Stack class const stack = new Stack() stack.push(4) stack.push(6) console.log( stack.pop()) console.log(stack.peek()) console.log(stack.isEmpty()) Console.log(stack.size())
results:
about object-oriented:
It is to decompose the things that build the problem into several objects .
The object is not established to complete a certain step, but to
describe the behavior of something in the process of solving the problem.
- push (element ) adds one or more elements to the top of the stack
- pop () deletes the top element and returns the removed element
- peek () returns the element on the top of the stack
- isEmpty () is used to determine whether the stack is Empty, empty is empty
- clear() is used to clear the elements of the stack
- size() is used to return the number of elements in the stack
- toString() is used to print the stack in the form of a string
. Then when implementing this class, we use objects to Simulation stack
class Stack { constructor() { this.count=0 this.items = {} } push(element) { this.items[this.count]=element this.count++ } pop() { if(this.isEmpty()){ return undefined } this.count-- const result=this.items[this.count] delete this.items[this.count] return result } peek() { if(this.isEmpty()){ return undefined } return this.items[this.count-1] } isEmpty() { return this.count===0 } clear() { this.items={} this.count=0 } size() { return this.count } toString(){ if(this.isEmpty()){ return undefined } let objectString=`${this.items[0]}` for(let i=1;i<this.count;i++){ objectString=`${objectString},${this.items[i]}` } return objectString } } const stack = new Stack() stack.push(23) stack.push(34) stack.push(80) console.log( stack.pop()) console.log(stack.peek()) console.log(stack.isEmpty()) console.log(stack.size())When
console.log(stack.toString())
uses objects to simulate stacks, it uses key:value
to store data. For example, this.items[this.count]=element uses
this.count to record in this structure. The size of the stack.
When we insert a number into it, we assign count as the key
and insert the value as the value. At this time, you need to change this.count++.
Regarding pop() and peek(), the toString() method needs to
first determine whether the stack is empty. If it is empty, it will return undefined.