Implementation of the Stack
To implement a stack, it is imperative to decide on the underlying data structure that stores it. The array is used here. Our implementation begins by defining the constructor of the Stack class:
function Stack () {
This.datastore = [];
this.top = 0;
This.push = push;
This.pop = Pop;
This.peek = peek;
}
We use the array DataStore to save the elements inside the stack, and the constructor initializes it to an empty array. The top position of the variable top record stack is initialized to 0 by the constructor, representing the starting position of the array at the top of the stack 0. If an element is pressed into the stack, the value of the variable will change. First to implement the push () method. When a new element is pressed into the stack, it needs to be stored in the position of the variable top in the array, and the top value is added 1 to the next empty position in the array. The code looks like this:
function push (Element) {
this.datastore[this.top++] = element;
}
Pay special attention to the location of the + + operator, which is placed behind the this.top so that the new stack element is placed in the position of the current value of top, and then the value of the top of the variable is added 1, pointing to the next position. The Pop () method is exactly the opposite of the push () method-it returns the top element of the stack while reducing the value of the top of the variable by 1:
function Pop () {
return this.datastore[--this.top];
}
The Peek () method returns an element of the top-1 position of the array, that is, the top element of the stack:
function Peek () {
return this.datastore[this.top-1];
}
If you call the Peek () method on an empty stack, the result is undefined. This is because the stack is empty and the stack top does not have any
Elements.
Sometimes you need to know how many elements are stored in the stack. The length () method returns the number of elements in the stack by returning the top value of the variable:
function Length () {
return this.top;
}
Finally, you can set the value of the variable top to 0 to easily empty a stack:
function Clear () {
this.top = 0;
}
Code induction
function Stack () {
This.datastore = [];
this.top = 0;
This.push = push;
This.pop = Pop;
This.peek = peek;
This.clear = clear;
this.length = length;
}
function push (Element) {
this.datastore[this.top++] = element;
}
function Peek () {
return this.datastore[this.top-1];
}
function Pop () {
return this.datastore[--this.top];
}
function Clear () {
this.top = 0;
}
function Length () {
return this.top;
}
Application of Stacks
Mutual conversion between the numbering unit
A stack can be used to convert a number from one to another. The false idea is to convert the number n to a number based on B, and the algorithm to implement the conversion is as follows.
The highest bit is n B, which pushes this bit into the stack.
Use n/b instead of N.
Repeat steps 1 and 2 until n equals 0, and there is no remainder.
Continue to eject the elements of the stack until the stack is empty, and then arrange the elements in order to get the string form of the converted numbers.
Using stacks, implementing the algorithm in JavaScript is a piece of cake. Here is the definition of the function, which converts the number to a number from two to nine:
function mulbase (num, base) {
var s = new Stack ();
do {
S.push (num% base);
num = Math.floor (num/= base);
} while (num > 0);
var converted = "";
while (s.length () > 0) {
Converted + = S.pop ();
}
return converted;
}
Turn from: 1190000004920420
Vagor
JavaScript and data Structure series (i)--implementation of stack