Data structure (Program design = data structure + algorithm)
A data structure is a relationship, yes, a collection of one or more specific relationships that exist between the elements.
Traditionally, we have divided the data structure into logical structure and physical structure.
Logical Structure: refers to the relationship between data elements in the data object, and it is the problem that we need to pay close attention to and discuss in the future.
Physical Structure: refers to the storage form of the logical structure of data in a computer.
The commonly used data structures are:
数组,队列(queue),堆(heap),栈(stack),链表(linked list ),树(tree),图(graph)和散列表(hash)
Stack: operations are performed only at one end of the table; queue: Operations are performed only at both ends of the table.
A queue is a linear table that allows only one end of an insert operation, while a delete operation at the other end.
In contrast to stacks, a queue is a linear table of FIFO (first-in-out, FIFO).
The same as the stack, the queue is also an important linear structure, the implementation of a queue also requires a sequential table or linked list as the basis.
Four structures
Collection structure
Linear structure
Tree-shaped structure
Graphic structure
Sequential storage and chained storage
There are two types of storage structures for data elements: sequential storage and chained storage.
For example, the array structure of our programming language is such a drop.
Chained storage structure: The data element is stored in any storage unit, this group of storage units can be continuous, or can be discontinuous.
Chained storage structure
Linear table
Linear table: Like a queue, a structure with a line-like nature, which is a finite sequence of 0 or more data elements.
If there are multiple elements, the first element has no precursor, and the last element has no successor, and the other elements have only one precursor and successor.
If the linear memento is (A1,..., ai-1,ai,ai+1,... An), the ai-1 in the table is ahead of Ai,ai, which is called ai+1 is the direct precursor element of AI, Ai-1 is the direct successor of AI.
Data type
Data type: Refers to a set of values of the same nature and the general name of some operations defined on this collection.
For example, the integer type of many programming languages, float type, character type these refer to the data type.
In the computer, memory is not infinite, if you want to calculate or deal with some large number, need to open up a large amount of memory space, so you have to classify the computer data type, a variety of data types to fit a variety of different computing conditions.
In the C language, data types can be divided into:
Atomic type: a basic type that can no longer be decomposed, such as Integer, float, character, and so on.
struct type: a combination of several types that can be decomposed, such as an integer array consisting of several integer data.
Algorithm
An algorithm is a description of the solution steps for a particular problem, represented as a finite sequence of instructions in a computer, and each instruction represents one or more operations.
The algorithm has five basic characteristics: input, output, poor, deterministic and feasible.
output: The algorithm has at least one or more outputs.
Poor: means that the algorithm automatically ends without an infinite loop after performing a limited number of steps, and each step is completed within an acceptable time.
determinism: Each step of the algorithm has a definite meaning and does not appear to be two semantic.
feasibility: Each step of the algorithm must be feasible, that is, each step can be accomplished by performing a limited number of times.
correctness: The correctness of the algorithm is that the algorithm should have at least input, output and processing without ambiguity, can correctly reflect the needs of the problem, can get the correct answer.
The time it takes for a program written in a high-level language to run on a computer depends on the following factors:
1. 算法采用的策略,方案2. 编译产生的代码质量3. 问题的输入规模4. 机器执行指令的速度
We can imagine that the linear table has two physical storage structures: The sequential storage structure and the chained storage structure.
Sequential storage structure of linear tables, which refers to the data elements of a linear table stored sequentially by a storage unit with successive addresses.
The linear table (A1,a2,..., an) is stored sequentially as follows:
Structure of linear table sequential storage
#define MAXSIZE 20 typedef int ElemType;typedef struct{ ElemType data[MAXSIZE]; int length; // 线性表当前长度} SqList;
In summary, the sequential storage structure package requires three properties:
The starting position of the storage space, array data, where it is stored is the storage location of the linear table storage space.
Maximum storage capacity of a linear table: the length of the array is maxsize.
Current length of the linear table: length.
The idea of inserting an algorithm
如果插入位置不合理,抛出异常; 如果线性表长度大于等于数组长度,则抛出异常或动态增加数组容量; 从最后一个元素开始向前遍历到第i个位置,分别将它们都向后移动一个位置; 将要插入元素填入位置i处; 线性表长+1。
JS data structure and algorithm storage structure