series

  • Using JavaScript learning data structures and algorithms (1) | small volume of free learning
  • Using JavaScript learning data structures and algorithms (2) | small volume of free learning
  • Using JavaScript learning data structures and algorithms (3) | small volume of free learning
  • (4) using JavaScript learning data structure and algorithm booklet | free education

The stack

A Stack is an ordered collection that follows the principle of first in, last out. New elements added or to be removed are stored at the end of the station, called the top of the stack, and the other end is called the bottom of the stack. In the stack, the new elements are near the top of the stack, and the old elements are near the bottom of the stack.

For example: a pile of books, or dishes in a restaurant.

// stack first in, then out
class Stack{
        constructor(){
          this.items = []
        }
        push(item){
           this.items.push(item)
        }
        pop(){
          return this.items.pop()
        }
        size(){
          return this.items.length
        }
        clear(){
          this.items = []
        }
}
let stack = new Stack()
stack.push(1)/ / into the stack
stack.push(2)/ / into the stack
stack.push(3)/ / into the stack
stack.push(4)/ / into the stack
stack.push(5)/ / into the stack
console.log(stack.size());/ / 5
console.log(stack.pop());/ / out of the stack
console.log(stack); / / [1, 2, 3, 4]
console.log(stack.size());/ / 4
Copy the code

The queue

A Queue is an ordered item that follows the last in, first out (FIFO) principle. Queues add new elements at the end of the queue and remove elements from the top. The most recent element must be at the end of the queue.

Example: waiting in line at a restaurant.

  • The priority queue sets the priority and then adds the element in the correct place
  • Each execution of a loop queue removes the first element of the queue and places the tail.

The list

Linkedlists store an ordered collection of elements, each consisting of a node that stores the element itself and a reference (also known as a pointer or link) to the next element.

Like a scavenger hunt or a series of cars on a train.

Example:

// {element: 'A', next:{element: 'B', next:{.. }}}
(A) => (B) = > C;
// Insert E between A and B
(A) = > (E) = > (B) = > C;
/ / delete the E
(A) = > (B) = > C;
Copy the code
  • Add elements
    1. Point A’s next to E
    2. Point E’s next to B
  • Remove elements
    1. Point A’s next to B
    2. Point B’s next to C

Two-way linked list

Linked lists are bidirectional, with one element linked to the next and one element linked to the next.

Circular linked list

Each element is linked not only to the next element and the last, but also to the elements at the head and tail, forming a closed loop.

head.prev = tail.next;
Copy the code

Photo source:

  • Nuggets – Learn data structures and algorithms in JavaScript

A collection of

A collection is made up of an unordered and unique (that is, unrepeatable) set of items. This data structure uses the same mathematical concept of a finite Set. In mathematics, a Set is a Set of different objects. You can think of a Set as an array with no repeating elements and no order. But members are unique.) . Some operations on collections:

  • Union: Given two sets, returns a new set containing all the elements of both sets.
  • Intersection: For a given two sets, return a new set containing elements in both sets
  • Difference set: For a given two sets, return a new set of all elements that exist in the first set and do not exist with the second set
  • Subset: Given two sets, verify that one set is a subset of another element.

Original from Ninety: links to original blog posts

This article is part of the “Gold Nuggets For Free!” Event, click to view details of the event