Order
Data structure and algorithm JavaScript This book is more simple to explain, the advantage is to use JavaScript language to describe the commonly used data structure, the book many examples from a number of common interview topics, is to keep pace with the times, amateur looked at the way to record down
git code Download: https://github.com/JsAaron/data_structure.git
Stack structure
Special lists, the elements within the stack can only be accessed through one end of the list, the top of the stack
Back-in-first out (lifo,last-in-first-out) data structure
JavaScript provides an operable way to push through the stack and pop out of the Stack, but POPs will remove the data from the stack
Implementation of a Stack implementation class
The underlying storage data structure is based on an array
Because POPs is the deletion of data in the stack, you need to implement a lookup method peek
Implement a Cleanup method clear
Amount of elements in the stack find length
Find out if there are still elements empty
Copy Code code as follows:
function Stack () {
This.datastore = []
this.top = 0;
This.push = push
This.pop = Pop
This.peek = Peek
this.length = length;
}
function push (Element) {
this.datastore[this.top++] = element;
}
function peek (element) {
return this.datastore[this.top-1];
}
function Pop () {
return this.datastore[--this.top];
}
function Clear () {
This.top = 0
}
function Length () {
Return This.top
}
Palindrome
A palindrome is a word, an array, a phrase, which is the same as the back and forth from the front 12321.ABCBA.
Palindrome The simplest idea is that the element inversion if the original element is equal, then it means that this is a palindrome
Here you can use this stack class to manipulate
Copy Code code as follows:
function Ispalindrome (word) {
var s = new Stack ()
for (var i = 0; i < word.length; i++) {
S.push (Word[i])
}
var rword = "";
while (s.length () > 0) {
RWord + + s.pop ();
}
if (Word = = RWord) {
return true;
} else {
return false;
}
}
Ispalindrome ("Aarra")//false
Ispalindrome ("Aaraa")//true
Look at this ispalindrome function, in fact, by calling the Stack class, and then passing in Word this element to the decomposition of each unit into the stack, according to the stack principle, after the first out of the principle, through the pop method in the reverse assembly of this element, At the end of the comparison and after the assembly, if the equivalence is a palindrome
Recursion
Using recursion to implement a factorial algorithm
5! = 5 * 4 * 3 * 2 * 1 = 120
Using recursion
Copy Code code as follows:
function factorial (n) {
if (n = = 0) {
return 1;
} else {
return n * factorial (n-1);
}
}
Using stack operations
Copy Code code as follows:
function fact (n) {
var s = new Stack ()
while (n > 1) {
[5,4,3,2]
S.push (n--);
}
var product = 1;
while (s.length () > 0) {
Product *= s.pop ();
}
return product;
}
Fact (5)//120
Using the while to decrement n = 5 into the stack, and then through a loop or based on the back of the stack in the first out of the principle, through the pop method to the front of the removal of the product overlay