Stacks are an ordered set of rules that follow a last-in, first-out (LIFO) rule, such as a stack of books on a table that we can only put or fetch from.
A queue is an ordered set of rules that follow a FIFO rule, such as queuing up first and leaving first.
Arrays are also an ordered set, similar to the above two data structures. Because the arrays in JavaScript have many simple methods, such as push (), it is easy to use arrays to represent stacks and queues.
First review the relevant methods:
Push (): Adds one or more items to the tail of the array.
Pop (): Deletes an item at the tail of an array.
Unshift (): Adds one or more items to the first array header.
Shift (): Deletes an item in the head of an array.
Represents the stack with an array. The tail of the array, the end of the stack, is called the top of the stack, and the other end is the stack. The more elements that are added later, the closer to the top of the stack, the stack is focused on the top of the stack.
The constructor of the stack
function Stack () {
//Initializes an array of varItems = []; //into the stack This. Push =function (elem) {Items.push (elem); }; //out of the stack, and returns the element This. Pop =function () {returnItems.pop (); }; //returns the top element of the stack This. Peek =function () {returnItems[items.length-1]; }; //whether the stack is empty This. IsEmpty =function () {return!items.length; }; //returns the size (length) of the stack This. Size =function () {returnitems.length; }; //Empty Stack This. Clear =function () {Items= []; }; //Print Stack This. Print =function () {Console.log (items.tostring ()); }; }
The above is a stack constructor, then we instantiate a stack.
var New Stack (); Stack.push ("Jack"); Stack.push ("Mike "); Stack.push ("Candy"// Jack,mike, Candy// jack,mike
Stack is an instance of a stack, jack,mike,candy sequentially into the stack, and then deletes a stack-top element (Candy) to get jack,mike.
In addition to simulating examples of life, stacks can solve some of the problems in computer science. Here's an example--decimal conversion to binary.
Binary is every binary one, so we just keep dividing the decimal number by two until the divisor equals 0, and each of the remainder (0 or 1) is sorted in order and is the corresponding binary number.
The following is implemented in code:
functionStack () {//Ibid . };functiondivideBy2 (num) {varstack =NewStack (),//Instantiate a stackresult = "",//used to store the final binary numberrem//used to store the remainder while(num > 0) {REM= Math.floor (num% 2); Stack.push (REM); Num= Math.floor (NUM/2); } //take the top element of the stack, stored in result, that is, a reverse sort of process while(!Stack.isempty ()) {Result+=Stack.pop (). toString (); } returnresult; }
Represents a queue with an array. A queue is an element that is added from the bottom and removed from the tail.
//QueuefunctionQueue () {//initializing an array varItems = []; //into the queue This. Enqueue =function(elem) {Items.push (elem); }; //out of the queue and returns the element This. dequeue =function () { returnItems.shift (); }; //returns the top element of the queue This. Front =function () { returnItems[0]; }; //determines whether the empty This. IsEmpty =function () { return!items.length; }; //return Queue Length This. Size =function () { returnitems.length; }; //print queue This. Print =function() {Console.log (items.tostring ()); }}
Instantiation:
var New Queue (); Queue.enqueue ("Jack"); Queue.enqueue ("Mike"); Queue.enqueue ("Candy" // jack,mike,candy// Mike,candy
Priority queue first. The priority queue is the upgraded version of the queue, each element in the queue has a priority, high priority is placed at the top of the queue, and the lower priority is placed at the tail of the queue. It is as if online tickets need to queue up, and members have higher priority, will be ranked in front of ordinary users.
Here is the code implementation:
//Priority QueuefunctionPriorityqueue () {//initializing an array varItems = []; //constructors for priority elements functionPriorityelem (Elem, priority) { This. Elem =Elem; This. Priority =Priority ; }; //into the queue This. Enqueue =function(Elem, priority) {Elem=NewPriorityelem (Elem, priority);//Priority Elements if( This. IsEmpty ()) {//if the queue is empty, go directly into the queueItems.push (Elem); } Else { varAdded =false; for(vari = 0; i < items.length; i++) { //before being inserted into a low-priority (precedence-Large) element if(Elem.priority < Items[0].priority) {items.splice (I,0, Elem); Added=true; Break; } } //there is no lower precedence than the element, the element is added to the trailer if(!added) {Items.push (elem); } } }; This. Print =function() {Console.log (items[3]); }};
Instantiation:
var New priorityqueue (); Queue.enqueue ("Jack", 2), Queue.enqueue ("Mike", 3); Queue.enqueue (" Candy ", 1// candy,jack,mike
Queues are prioritized from high to low (priority from small to large)
Welcome to add or correct
JavaScript implements stacks and queues with arrays