JavaScript implementation structure stack (Stack)
I. Introduction
1.1 What is a data structure?
It is a data structure in a computer, store and organize data.
For example: library management, how to put books in order both to put a lot of books, but also easy to take?
The main two questions to consider:
- Operating one: how insert new book?
- Operation II: how to find a specific book this?
Common data structure:
- Array (Aarray)
- Stack (Stack)
- List (Linked List)
- Figure (Graph)
- Hash table (Hash)
- Queue (Queue)
- Tree (Tree)
- Heap (Heap)
Note : Data Structures and Algorithms and language-independent, common programming languages have a direct or indirect use of the common data structure.
1.2. What is an algorithm?
Algorithm (Algorithm) of the definition
- A finite set of instructions, each instruction is described does not depend on the language;
- Receiving some input (input not required in some cases);
- Generating an input;
- Terminated after a certain limited steps;
Popular understanding algorithms: solution to the problem / logical step. Implementing data structures, algorithms can not be separated.
Second, the structure of the stack (Stack)
2.1 Introduction
An array is a linear structure, and may be in the array anywhere element insertion and deletion. The stacks and queues is more common linear structure is limited . As shown below:
Stack is characterized by last-out, last in first out (LIFO: last in first out) .
Program stack structure:
Function call stack : A (B (C (D ()))): A function that is called B, B calls C, C calls D; A will be pushed onto the stack during the execution of A, B is then executed when B is also pushed onto the stack, it will be pushed onto the stack when the function C, and D performed. Therefore, the order of the current stack is: A-> B-> C-> D ( top of the stack); after executing the function D will pop the stack to be released, pop the stack in the order of D-> C-> B-> A;
Recursive : Why did not stop recursion conditions can cause a stack overflow? A function such as a recursive function, continue to call themselves (because the function has not been performed, the function does not pop the stack), kept the same function A onto the stack, and finally cause a stack overflow (Stack Overfloat)
3. Exercise: Title: There are six elements 6,5,4,3,2,1 sequentially into the stack, and asked which of the following is not a legal order of the stack?
- A:5 4 3 6 1 2 (√)
- B:4 5 3 2 1 6 (√)
- C:3 4 6 5 2 1 (×)
- D:2 3 4 1 5 6 (√)
Title of said sequence into the stack does not refer to all at once into the stack, but there is a carry out there, into the stack order of 6 -> 5 -> 4 -> 3 -> 2 -> 1.
Resolution:
- A Answer: 65 into the stack, the stack 5, 4 out of the stack into the stack, the stack 3 into the stack, the stack 6, 21 into the stack, a stack, the stack 2 (Drawing entire sequence follows 654321);
- Answer B: 654 into the stack, the stack 4, the stack 5, 3 out of the stack into the stack, the stack 2 into the stack, the stack into the stack 1, stack 6 (the whole sequence follows Drawing 654321);
- C A: 6543 into the stack, the stack 3, a stack 4, stack 5 should then instead of six, so the error;
- D Answer: 65,432 into the stack, the stack 2, 3 stack, the stack 4, the stack 1 into the stack, the stack 5, 6 stack. In line with the order of the stack;
Stack common operations:
- push (element): Add a new element to the stack position;
- pop (): removing the element stack, and returns the element removed;
- peek (): Returns the element top of the stack, the stack does not make any changes (this method does not remove the element stack, just return it);
- isEmpty (): returns true if the stack is not any element, otherwise return false;
- size (): Returns the number of elements in the stack. The length property similar methods and arrays;
- toString (): returns the contents of the stack structure as a string.
2.2 Encapsulation stack class
Code:
// 封装栈类
function Stack(){
// 栈中的属性
this.items =[]
// 栈的相关操作
// 1.push():将元素压入栈
//方式一(不推荐):给对象添加方法,其他对象不能复用
// this.push = () => {
// }
//方式二(推荐):给Stack类添加方法,能够多对象复用
Stack.prototype.push = function(element) {
// 利用数组item的push方法实现Stack类的pop方法
this.items.push(element)
}
// 2.pop():从栈中取出元素
Stack.prototype.pop = () => {
// 利用数组item的pop方法实现Stack类的pop方法
return this.items.pop()
}
// 3.peek():查看一下栈顶元素
Stack.prototype.peek = () => {
return this.items[this.items.length - 1]
}
// 4.isEmpty():判断栈是否为空
Stack.prototype.isEmpty = () => {
// 两个小时的教训啊不是this.length(不是Stack对象的length,Stack类没有length属性啊),而是 Stack类中定义的数组items才有length属性呀
return this.items.length == 0
}
// 5.size():获取栈中元素的个数
Stack.prototype.size = () => {
return this.items.length
}
// 6.toString():以字符串形式输出栈内数据
Stack.prototype.toString = () => {
//希望输出的形式:20 10 12 8 7
let resultString = ''
for (let i of this.items){
resultString += i + ' '
}
return resultString
}
}
Test code:
// 栈的使用
let s = new Stack()
s.push(20)
s.push(10)
s.push(100)
s.push(77)
console.log(s) //65
console.log(s.pop()); //68
console.log(s.pop()); //69
console.log(s.peek()); //71
console.log(s.isEmpty()); //72
console.log(s.size()); //74
console.log(s.toString()); //75
Test Results:
Simple application stack structure:
Stack structure using the features of the package to be converted to decimal to binary function:
//简单应用:
//封装函数:将十进制转成二进制(十转二的运算最后倒叙取余的特点符合栈'先进后出')
let dec2bin = decNumber => {
//1.定义一个栈对象,保存余数
var stack = new Stack()
// 2.循环操作
while(decNumber > 0){
// 2.1.获取余数并放入栈中
stack.push(decNumber % 2)
// 2.2.获取整除后的结果作为下一次运算的数字(floor:向下取整)
decNumber = Math.floor(decNumber / 2)
}
// 3.从栈中取出0和1
let binaryString = '';
let a = stack.items.length
while(stack.items.length != 0){
binaryString += stack.pop();
}
return binaryString;
}
//测试代码
console.log(dec2bin(10)); //103
console.log(dec2bin(100)); //104
console.log(dec2bin(1000)); //105
Test Results: