JS array data structure

I. Introduction

When we talk about stacks and queues, it will reflexively think of the data structure. In ECMAScript standard, the array is also provided a method similar to other data structures, namely the stack method and the queue method .


Second, the stack method

1. Definitions

Stack is a LIFO data structure, which is the most recently added items will be removed first, like a stack of pancakes in general.

Insertion (also known as push) and removal of the items in the stack (also known as pop-up) occurring only at the top of the stack .

There are two methods provided stack, are push and pop methods method.

  • push method: the end of the array pushed key, and returns the length of the new array modified.
  • pop method: the end of the array pop item, length of the array is decreased by one, and returns the item removed.

2. Example

var arr = [];
// push方法
var count = arr.push('A', 'B');
console.log(count); // 2
console.log(arr); // ["A","B"]
count = arr.push('C');
console.log(count); // 3
// pop方法
var item = arr.pop();
console.log(item); // "C"
console.log(arr.length); // 2
复制代码

Third, the queue method

1. Definitions

Access Rule queue data structure is a first in first out , after a top than a first went out, like bullets collide in general.

Insertion and removal occurs in the operating position the head of the team .

The method provides two queues, namely a method unshift and shift methods.

  • unshift Method: the front end of the array is added items, and returns the length of the new array
  • shift method: the front end of the array to delete items, length minus one array of values, and returns the deleted items

2. Example

var arr = ['A','B','C'];
// unshift方法
var count = arr.unshift('a');
console.log(count); // 4
console.log(arr); // ["a", "A", "B", "C"]
// shift方法
var item = arr.shift();
console.log(item); // "a"
console.log(arr.length); // 3
复制代码

Fourth, the mind map section

Source Address: github.com/Knight174/M...

Guess you like

Origin blog.csdn.net/weixin_34257076/article/details/91399434