Preliminary concept of the foundation and data structure of C language recursive

xiaoxiao2021-03-06  214

(1) C language functions and parameters

Before starting to learn the data structure and the algorithm design method, we will review some C language bases, which will help us based on C language programming and analysis, although there are many you may be very familiar.

1.1 function and parameters (pass value, reference)

[1] Parameters of the Value Value: Investigating Functions EX_1 (Program 1.1), A, B, and C are all formal parameters of the function EX_1, the type is shaped, if the following calls EX_1 (1, X), it The return result is still an integer:

-------------------------------------- Program 1.1Int EX_1 (Int A, Int B, INT C) {RETURN (A B) / C;

When EX_1 (1, 2, x) is executed, the value of A is assigned to the first parameter 1, and the value of B is assigned to the second parameter 2, but it should be noted that the value of c is assigned to x, but if x is not int type, first, the mandatory type conversion will be performed first. For example, X = 3.7 is a FLOAT type, then C will be assigned to INT (3.7) = 3. In this block (program 1.1), A, B, and C are all transmitted parameters, but when this form is transmitted, the type of the original parameter can be judged to be a, b, and c The same type is the same, if the mandatory type conversion is performed different, it is assumed here that this conversion is allowed. In this form of incoming parameters, the function body call or operation is just a copy of the original parameter without modifying the value of the original parameter.

[2] Quote Parameters: For writing and understanding, we will not be described in the form of c reference delivery parameters in the program segment. The usage of the programs used in the program 1.1 increases the overhead of the program, such as calling EX_1 (int A, int b, int c), when the parameter is incorporated into the parameter, but it is necessary to release these in return Copy, assuming that the function EX_1 () is called one thousand times, then copy and release the pilot value parameters A, B, C each each, obviously this will lead the efficiency reduction! Use reference parameters will be a good way to solve this problem.

--------------------------------------- Procedure 1.2int EX_2 (Int & A, Int & B, INT & C) {RETURN (A B) / C;

The functions ex_2 (x, y, z) in the inspection process 1.2. The types of X, Y, Z are the same as those in EX_2, respectively, these raw parameters x, y, and z will be assigned the alias (Alias) A, B, C. It is also equivalent to the three people who have called X, Y, Z, and give them another name, respectively, A, B, C called them. Therefore, in the function EX_2 (), the operations of A, B, and C are equivalent to the original X, Y, Z directly, but the name of the current name is now different. Moreover, reference parameters can only vary without passing, such as calling EX_2 (1, x, y) is illegal, and is different from the transmitted value parameters, the block (program 1.2) does not have additional copying three parameters when calling Copy, the function is returned, and it has not released them again, thus improving efficiency. However, it is important to note that the reference parameters change the value of the original parameter, and the parameter parameters are not, so when the parameter transmission form is selected, special attention is required to distinguish between the differences.

--------------------------------------- Procedures 1.2.1void Swap (Int & A, Int & B ) {INT TEMP; TEMP = a; a = b; b = Temp;} program 1.2.2void swap_err (int A, int b) {int TEMP; TEMP = a; a = b; b = Temp;} --- ------------------------------------

The above program segment is to complete the function of switching two variables, in program 1.2.1 is a reference parameter, and the program 1.2.2 is a transmission value parameter. You can experiment, SWAP successfully exchanges two variables, and Swap_err with the pilot value parameter is wrong.

Differences of passing values ​​and reference parameters (Table 1.1) | -------------- | Command | Terminal || Call Efficiency | Low | High || Change the original parameters | No | Yes | 1.2 Function and Recursive Recursive Function is a function that calls your own yourself. The recursive function includes two, direct recursive, and indirect recursive (INDIRCET Recursive). Direct recursive means that the code directly contains the statement that calls f directly; the indirect recursive means that the function f calls the function g, and G call H, so that the F is called. For a recursive definition of function f (n) (assuming is direct recursive), you must make it a complete definition, you must meet the following two conditions: • You must contain a basic part (BASE), which is the recursive termination condition. Recursive termination conditions may have one or more. • In the recursive component, the parameters of this section must be repeated until f is the basic portion of F, that is, continuous recipients until the recursive function is terminated. 1.2.1 Examples (1) For example, a mathematical formula for N! (Equation 1.1):

1 [n <= 1] f (n) = n * f (n - 1) [n> 1]

In this formula (Equation 1.1), the basic part is that the recursive termination condition is 1 [n <= 1], and the recursive portion is N * f (n - 1) [n> 1], the write is a C program is

--------------------------------------- Procedure 1.3 / * f (n) = n! See Mathematical Equation 1.1 * / INT F (INT & N) {if (n <= 1) Return 1; // Recursively Terminal Part ElseretURN N * f (n - 1); // Removable section, repeatedly modify the parameters until recursive termination } --------------------------------------- Assumption call f (4) ask 4! The value of the recursive process is: f (1) = 1 // Remature termination return value f (2) = 2 * f (1) // Use F (1) return result again to find f (2) = 2 * f (1) = 2 value and return F (3) = 3 * f (2) // Upstrated, the return value directly needs to utilize the calculation result f (4) = 4 * f (4) = 4 * f ( 3) // f (3) The value is not known, so the value of the recreation F (3) is f (4) = 24.

1.2.2 example (2)

Look at the recursive example of the Fibonacci number, mathematical formula (Equation 1.2) is: 1 [n <= 1] FIB (n) = 1 [n = 2] FIB (n - 1) FIB (N - 2) [N> 3] The base portion (stop condition) of the formula (Equation 1.2) is 2, which is 1 [n <= 1], and 1 [n = 2], but can also be combined into 1 [n <= 2] foundation: --------------------------------------- Procedure 1.4 / * f (n) is the value of the Fibonacci number, see Mathematical Equation 1.2 * / INT F (INT & N) {if (n <= 2) Return 1; // Removal Termination Part Elsereturn FIB (n -1) * FIB (n - 2); // Recursively modified parameters until recursive termination} -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------

The recursive process of program 1.4 is also similar, and the recursive parameters are constantly modified until the termination conditions are met, then the required FIB (N) of the last returned FIB (N) is returned from the lowest layer at the termination condition. If DEST = FIB (4) is called; please simulate the recursive process and the return value therebetween.

1.2.3 Recursive and non - recursion distinction (Supplementary part)

Any "recursive program" can be converted to the "non-recursive program" implementation by programmers (Note: The recursive program cannot be equivalent to the recursive mathematical formula). Moreover, not all recursive mathematical formulas can be converted to non-recursive formulas, such as the proven dual-intensive function (such as Ackmann functions) does not exist non-recursion mathematical formulas. However, some recursive formulas can be converted to non-recursive, such as the value of N!, Can actually be converted to a recurrent formula to solve the higher efficiency for computer programs. It can be seen that the obvious benefits of programming using the recursive function is clear, because recursive usually has strict mathematical formulas, and that it is easy to use the recursive procedure or algorithm to prove its correctness (but here Multi-narrative). At the same time, it is also necessary to see that the recursive procedure is obviously in the event of a function of recursive, it needs to be saved / recovered in memory, which will greatly increase the overhead of the system. However, because the speed of the modern computer and the increasing memory capacity are increasing, these overhead is sometimes not very important. Therefore, when you select the recursive structure, the relationship between "understandable and efficiency" is considered.

2) What is a variety of data structural data structures to study data, data types and other relationships between the data, which mainly include the nature of various data structures, that is, their physical structure, logical structure, and apparatus The operation is on. The data structure is the carrier of information, the algorithm is the step of processing information, and can generally be considered: the program = algorithm data structure.

Classification of data structures (Table 1.2)

Simple Type Integer, Real, Character, Boolean Static Data Type Construction Type Array, Structural, Collection, String Static Data Type File, Pointer Dynamic Data Type Linear Structure Arrse, Link Picture, Stack, Queue, String --- --- Nonlinear tree, illustration ------

[Note: The following article is included in Starfish] 1. What is the data structure? The data structure is a widely used term throughout computer science and technology. It is used to reflect the internal composition of a data, that is, a data consists of those components data, which is constituted, what is the structure. The data structure has logically data structures and physical data structures. The logical data structure reflects the logical relationship between the component data, and the physical data structure reflects the storage arrangement of the component data in the computer. The data structure is in the form of data. The data structure is an organizational mode of information, and its purpose is to improve the efficiency of the algorithm, which is usually corresponding to a set of algorithms, and the data in the data structure can be performed by this set of algorithms. 2. What is the main study of the data structure? Data Structure As a large number of logical structures and storage structures of the main research data, and various operations for data. Therefore, there are mainly three aspects: the logical structure of the data; the physical storage structure of the data; the operation (or algorithm) of the data. Typically, the design of the algorithm depends on the logical structure of the data, and the implementation of the algorithm depends on the physical storage structure of the data. 3. What is data? What is the structure? What is a logical structure and physical structure? Data refers to a collection of elements consisting of limited symbols (such as "0", and "1", with its own structure, operation, and corresponding semantics). The structure is a collection of relationships between elements. Generally, a data structure DS can be represented as a binary group: DS = (D, S), // IE, Data-Structure = (Data-Part, Logic-structure-part) D is a collection of data elements (Or "Node", may also contain "data item" or "data field"), S is a collection of relationships defined on D (or other collections), s = {r | R: D × D ×. ..}, called the logical structure of the element. There are four basic types of logical structures: collection structure, linear structure, tree structure, and network structure. Tables and trees are the most commonly used high-efficiency data structures, many efficient algorithms can be designed with these two data structures. Table is a linear structure (full order relationship), tree (predecessor or hierarchical relationship) and chart (WEAK / Local Orders) is a non-linear structure. The physical structure of the data structure refers to the storage mirroring of the logical structure. The physical structure P of the data structure DS corresponds to a mapping from the DS element to the storage area M (maintaining a logical structure S): P: (d, s) -> M memory model: a memory M is a series of fixed The size of the storage unit, each unit u has a unique address A (U), which is continuously encoded. Each unit U has a unique subsequent unit u '= SUCC (U). P 4 Basic Mapping Models: Sequential, Linked, Indexed, and Hashing mapping. Therefore, we can get at least 4 × 4 possible physical data structures: SEQUENTIAL (SETS) Linked Lists Indexed Trees Hash Graphs (not all possible combinations reasonable) 4. Operation on Data Structure (DS): All definitions Operation on the DS must maintain the logic and physical structure of DS when changing data elements (nodes) or nodes.

转载请注明原文地址:https://www.9cbs.com/read-128934.html

New Post(0)