Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Stack structure in JavaScript

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to achieve the stack structure of JavaScript, I hope you will learn something after reading this article, let's discuss it together!

First, understand the stack structure

We know that arrays are common data structures and can insert and delete data anywhere in the array, but sometimes we have to limit this arbitrariness in order to achieve certain functions. Stacks and queues are common limited data structures, so let's take a look at the stack first.

Stack (stack), which is a restricted linear table, last-in, first-out (LIFO)

The restriction is to allow insert and delete operations at one end of the table. This end is called the top of the stack, on the other hand, the other end is called the bottom of the stack.

LIFO (last in first out) represents the element that enters later, and the stack space pops up first.

Inserting a new element into a stack, also known as entering, entering, or pressing the stack, places the new element on top of the top element of the stack, making it a new top element of the stack

Removing an element from a stack, also known as making a stack or unstack, removes the top element of the stack so that its adjacent elements become the new top element of the stack.

The structure diagram is as follows:

Similar to the stack in life.

For example, when we type the code, if an error occurs and needs to be deleted, then the first one to be typed on is the last to be deleted.

Next, let's implement the encapsulation of the stack structure, which will be based on the array.

Second, stack structure packaging

First, create a class wrapper stack structure, as follows:

Function Stack () {}

Add properties and methods inside it, and add the array to the class through the methods of properties. Then use the prototype method to add common operations.

Common stack operations are:

Push (element): add a new element to the top of the stack.

Pop (): removes elements from the top of the stack

Peek (): returns the element at the top of the stack without making any changes to the stack

IsEmpty (): determines whether the stack is empty, returns true if there are no elements in the stack, otherwise returns false

Size (): returns the number of elements in the stack

ToString (): returns the contents of the stack structure as characters

Next, let's 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 element at the top of the stack pop () Stack.prototype.pop = function () {return this.items.pop () } / / returns the element at the top of the stack without making any changes to the stack peek () Stack.prototype.peek = function () {return 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 }} / / returns the number of elements in the stack size () Stack.prototype.size = function () {return this.items.length;} / / returns the contents of the stack structure in character form toString () Stack.prototype.toString = function () {var str ='; for (var I = 0) I0) {/ / get the remainder and put it in the stack stack.push (decNumber%2); / / get the new divisor decNumber = Math.floor (decNumber/2);} / / get the top element var str =''; while (! stack.isEmpty ()) {str + = stack.pop ();} return str } console.log (conversion from '100 to binary is:' + decToBin (100)); console.log (conversion from'50 to binary is:'+ decToBin (50)); console.log (conversion from'20 to binary is:'+ decToBin (20)); console.log (conversion from'34 to binary is:'+ decToBin (34))

The output is as follows:

After reading this article, I believe you have a certain understanding of "how to implement the stack structure of JavaScript". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report