Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

What is programming equal to?

The essence of Cheng Xun’s design is to select a good structure and design a good algorithm for determining the problem. So programming = data structure + algorithm (some people think that the nature of programming is just data structure, that algorithm is just a process of solving data structure).

Tips: Gartner created the data structure class

What is data?

Data are symbols that describe objective things, data = symbols

  1. It can be typed into a computer
  2. Can be recognized by a computer

Data elements, objects, items

  • 1. The basic unit of data, also known as a node or record.
  • Data object: A collection of data elements of the same nature, a subset of data (table object people table)
  • Data item: The smallest unit of data with independent meaning (table title [name])

The purpose of data is storage, the purpose of storage is: later reuse

Const a = [1, 2, 3] const Salary = 100 const Student = [' zhang ', 'Li ',' Wang '] console.log(a, Salary, Student)Copy the code

For storing data, we need to choose a good way to store data. For simple data, we can use variables or arrays to store data.

But if there is a complex data, for example:

Const dog = [' cheat philandering emotional man 'girlfriend' 1 ', 'girlfriend 2', '... ']Copy the code

We cannot reflect the logical relationship between data in an array like Dog, so it is impossible to use it later.

So how to store data with complex relationships is more conducive to the reuse of data in the later period?

This is where data structures come in, and the main purpose of data structures is to illustrate relationships.

What does structure mean?

  • The simple understanding is the relationship, different data elements are not independent, but there is a specific relationship between them.

Classification of data structures

Logical structure: The relationship between data elements in a data object

  • There are four kinds: set structure, linear structure, tree structure and graph structure.

Set structure:

Data elements belong to the same union, and there is no other relationship between them. Their common property is: “belong to the same set”.

Linear structure:

Linear structure is a collection of ordered data, the most typical view of data is one-to-one. Except for the first and last data elements, all data elements are primed.

Necessary conditions for forming linear structures:

  1. The first element must exist.
  2. The last element must exist.
  1. All data elements except the last one have a unique “subsequent”.
  2. All data elements except the first have a unique precursor.

Arrays, stacks, queues are all linear structures.

Tree structure:

The data elements are one-to-many (such as a DOM tree).

Graphic structure:

The data elements are many-to-many relationships (for example, navigation maps).

Physical structure: Memory in which data elements are stored in a computer.

In terms of memory, the data storage structure should correctly reflect the logical relationship between data elements.

  • There are two types: sequential storage and chain storage.

Sequential storage:

It’s like queuing.

Chain storage:

Pointer to the address of the data element.

Common data structures:

  • figure
  • push
  • Hash table
  • The tree
  • The list
  • An array of
  • The stack
  • The queue

Objects are not data structures: Objects expose behavior, while data structures expose data, and data structures are language-independent.

What exactly does an algorithm mean?

Algorithm is the method and step to solve the problem, and the realization of data structure can not be separated from algorithm.

An array of data structures

An array is a contiguous memory data structure consisting of a collection of elements of the same type.

What is an array in a data structure?

Array: A series of contiguous areas in memory that hold values. An array is an ordered collection of data elements of the same type.

When you add or remove elements from the middle of the array or from the head, the array object will automatically expand or shrink the memory to ensure that there are no gaps between the elements. When a large number of elements are moved, the efficiency is low, so try to add and remove elements from the end of the array.

The js array is not really an array because it does not meet the criteria for a true array.

Const mm=[' sweet ', 'fangfang ',' yuanyuan ', 1, 2, 3]Copy the code

Because js array elements can be of any type, and the memory address in JS is not contiguous.

Advantages of arrays:

1. Querying elements by index is fast

2. Store lots of arrays

3. Traverse the array according to the index

4. Convenient definition and flexible access

Disadvantages:

1. Searching by content is slow

2. Adding and deleting elements is inefficient

3. Array size, once determined, cannot be changed, not suitable for dynamic storage (has nothing to do with JS)

4. Arrays can only store the same type of data (independent of JS)