1. Understanding the stack structure We know that arrays are a common data structure, and data can be inserted and deleted at any position in the array. However, sometimes, in order to achieve certain functions, we must restrict this arbitrariness. Stacks and queues are relatively common restricted data structures. Let's take a look at the stack first.
Its structure diagram is as follows: Similar to stack in life
Next, let's implement the encapsulation of the stack structure together, and the method we will adopt is based on array. 2. Stack structure encapsulationFirst, create a class encapsulating the stack structure as follows: function Stack(){ } Add properties and methods inside it, and add the array to the class through the property methods. Then use the prototype method to add commonly used operations. Common operations on stacks are:
Next, we will implement them one by one: function Stack(){ this.items = []; // Add a new element to the top of the stack. push() Stack.prototype.push = function(element){ this.items.push(element); } // Remove the top element of the stack pop() Stack.prototype.pop = function(){ return this.items.pop(); } // Return the element at the top of the stack without making any modifications to the stack peek() Stack.prototype.peek = function(){ return this.items[this.items.length-1]; } // Determine whether the stack is empty isEmpty() Stack.prototype.isEmpty = function(){ if (this.items.length == 0) { return true; }else { return false; } } // Return the number of elements in the stack size() Stack.prototype.size = function(){ return this.items.length; } // Return the contents of the stack structure as characters toString() Stack.prototype.toString = function(){ var str = ''; for(var i =0;i<this.items.length;i++){ str += this.items[i] + ' '; } return str; } }
Finally, verify. The code is as follows: var stack = new Stack(); stack.push(1); stack.push(2); stack.push(3); stack.push(4); stack.push(5); console.log(stack); console.log('The top element of the stack removed is: '+stack.pop()); console.log('The top element of the stack is: '+stack.peek()); console.log('Is the stack empty: '+stack.isEmpty()); console.log('The number of elements in the stack is: ' + stack.size()); console.log('The contents of the stack structure are:'); console.log(stack.toString()); The output is: Build successful. 3. Convert decimal to binaryHow to convert decimal to binary? To convert decimal to binary, we can divide the decimal number by 2, push the remainder into the stack, until the result is 0, and finally pop the elements in the stack one by one to get the final result. As shown in the following figure: The specific code is: function Stack(){ this.items = []; //Push into the stack Stack.prototype.push = function(element){ this.items.push(element); } //Pop out Stack.prototype.pop = function(){ return this.items.pop(); } //Judge whether the stack is empty Stack.prototype.isEmpty = function(){ if (this.items.length == 0) { return true; }else{ return false; } } } function decToBin(decNumber){ var stack = new Stack; while(decNumber>0){ //Get the remainder and put it into the stack stack.push(decNumber%2); //Get the new divisor decNumber = Math.floor(decNumber/2); } //Get the top element of the stack var str = ''; while(!stack.isEmpty()){ str += stack.pop(); } return str; } console.log('100 converted to binary is: '+decToBin(100)); console.log('50 converted to binary is: '+decToBin(50)); console.log('20 converted to binary is: '+decToBin(20)); console.log('34 converted to binary is: '+decToBin(34)); The output is: This is the end of this article about the detailed process of You may also be interested in:
|
<<: Some ways to solve the problem of Jenkins integrated docker plugin
>>: Various methods to implement the prompt function of text box in html
Preface When a Linux is fully set up, you can use...
As shown in the figure: Table Data For such a tre...
I recently deployed Django and didn't want to...
There is a new feature that requires capturing a ...
(Win7 system) VMware virtual machine installation...
Front-end technology layer (The picture is a bit e...
In HTML, <, >, &, etc. have special mean...
I have been working on a project recently - Budou...
Table of contents 1. What is the life cycle 2. Th...
Table of contents 1. Operators Summarize 1. Opera...
name Specify a name for the tag. Format <input...
Preface Learn to create a file system on your sys...
There are also two servers: Preparation: Set the ...
Table of contents 1. Easy to read code 1. Unified...
1. What is a transaction? A database transaction ...