Stack, a type of data structure that follows the Last in, First out (LIFO) principle.
It is easy to understand the stack as a container similar to a ping-pong barrel. The bottom of the barrel is sealed, and the top is open. The transverse section of the barrel just accommodates the diameter of a ping-pong ball.
- The first ball in the stack is at the bottom of the bucket.
- The last ball to be put into the stack is at the top of the ping pong barrel (the top of the stack).
- If you want to get a ping-pong ball out of the stack, ball 4 must be the first thing you get.
- To get ball 1 out of the stack, you need to get all the balls above ball 1 before you get ball 1.
The function call stack in JavaScript is the stack data structure, which I won’t discuss much here. Now we are going to implement a stack data structure using emulation.
Analysis of the
- To use an object-oriented implementation, you first need to write one
Stack
Constructor. Stack
We need to have an array insideitems
Store all elements in the stack.- There needs to be a way to add elements to the stack
push
. - There’s also a way to remove the element, because the element to remove must be the top element, corresponding to the array
pop
Method, hence the same namepop
.
writeStack
Constructor:
function Stack(){
this.items = [];
}
Copy the code
addpush
Methods:
Stack.prototype.push = function(item){
this.items.push(item);
}
Copy the code
addpop
Methods:
Stack.prototype.pop = function(){
this.items.pop();
}
Copy the code
Can you giveStack
Class to add some common methods, such as:
Look at the top element of the stackgetTop
Methods:
Stack.prototype.getTop = function(){
return this.items[this.items.length-1];
}
Copy the code
View the number of stack elementssize
Methods:
Stack.prototype.size = function(){
return this.items.length;
}
Copy the code
Empty stackclear
Methods:
Stack.prototype.clear = function(){
this.items = [];
}
Copy the code
Printing of the stackprint
Methods:
Stack.prototype.print = function(){
console.log(JSON.stringify(this.items));
}
Copy the code
Complete code:
function Stack(limit){
this.items = [];
}
Stack.prototype.push = function(item){
this.items.push(item);
}
Stack.prototype.pop = function(){
this.items.pop();
}
Stack.prototype.getTop = function(){
return this.items[this.items.length-1];
}
Stack.prototype.size = function(){
return this.items.length;
}
Stack.prototype.clear = function(){
this.items = [];
}
Stack.prototype.print = function(){
console.log(JSON.stringify(this.items));
}
Copy the code
Create an instance and test it
var stack = new Stack();
// Insert some elements
stack.push('a');
stack.push('b');
stack.push('c');
// Check the stack elements
console.log(stack.size()); / / 3
// Get the top element of the stack
console.log(stack.getTop()); // "c"
/ / print
stack.print(); // ["a","b","c"]
// Push the top element off the stack
stack.pop();
// Check the stack elements
console.log(stack.size()); / / 2
// Get the top element of the stack
console.log(stack.getTop()); // "b"
/ / print
stack.print(); // ["a","b"]
Copy the code
At this point, the simulated implementation stack code test is complete.