I. Data Type:
In any programming language, the data type as a whole, the ANSI-C contains the types: int, double, char ......, Programmers are rarely satisfied with the data types provided by the language itself. A simple method is to construct a data type similar to array, struct, or union.
So what is the data type? We can define this as follows: a data type is a set of values. Generally, char has 256 different values, int has more values, and double has more values, but it is usually different from a real number in the mathematical sense.
Correspondingly, we can define the data type: A set containing some values, and add some operations on the values. Generally, these values can be expressed by computers, and more or less reflect feasible hardware commands for their operations. The int type in the ANCI-C is not doing very well in this regard: There are different values on different machines, and arithmetic right shift operations may also be different.
For example, we usually define the data structure of a linear structure as follows:
typedef node *
The following operations are defined:
node * head(node * elt, node * tail);
When we do not show the specific implementation to the user, it is called an abstract data type. For example, we can remove an element from a queue, and our colleagues can add an element to it in a certain order.
ABSTRACT Data Types provide programmers with the maximum flexibility. Because the definition does not contain specific implementations, we are free to choose any simple and efficient implementations.
ABSTRACT Data Types meet good programming principles:Information Hiding and Governance Policy
The information that represents the data item is only presented to the person who needs to know: For programmers but not users.
ABSTRACT Data Types allow us to easily isolate Program Development and Implementation: splitting a large task into a small module in our own way
Iii. Example -- Set
How can we build an abstract data type? A set contains the following operations: add, find, drop ......, They provide a set of elements and return the added elements. The find operation is used to tell us whether an element is in the set.
In this case, set is an abstract data type and declares our operations on set, starting with a Set. h header file:
SET_H * * add ( * , * * find ( * , * * drop ( * , * contains ( * , * *
Set will display the operations on sets to some extent. add () adds an element to the set and returns whether it already exists in the set or is successfully added. find () search for an element in set, and return a position or a null pointer. Drop () locates an element and removes it from the set. The system returns the removed element. Contains () converts the result of find () to a specific value.
The general pointer void * is always used throughout. On the one hand, it can hide some details of the set. On the other hand, it allows us to pass virtually any type to add () and other functions.
Iv. Memory Management
How to obtain a set? Set is a pointer instead of a type defined by typedef. As a result, we cannot define the Set type as a local variable or a global variable. Instead, we can define the following code in new. h by using a pointer to point to the elements in sets:
NEW_H * ( * delete ( *
V. Object
If we want to collect any data of interest in the set, we need another data type Object, which is described as follows in Object. h:
OBJECT_H * Object; differ ( * a, *
With the header file above, you can write out the following application main. c:
* S = * a = add (s, * B = add (s, * c = (contains (s, )&&View Code
VII. Implementation
The functions declared in all header files in this article will be implemented one by one:
Implementation -- Set
Main. c can be compiled successfully, but before compiling and executing the program, we must implement abstract data types and memory management. If an object does not store any information, and each object belongs to at least one set, then we can use a unique small positive integer value to represent the object and each set, and these positive integer values can be represented by an index in the array heap.
If an object is a set member, the corresponding array element contains an integer representing the set.
Sets has the same display as the object. new () does not care about the type description. It returns an element with a value of 0 in heap []. The Code is as follows:
! defined MANY || MANY < 1 MANY 10 * ( * * p; (p = heap + ; p < heap + MANY; ++ (! *< heap +* p =
0 is used to mark valid elements in heap []. As a result, we cannot return a pointer pointing to heap [0]-if it is set, its members can obtain a 0 index. New () may be out of bounds and can be avoided using assert. Elete () must be careful with the null pointer. A heap [] element is recycled by being set to 0:
delete ( * * item => heap && item < heap +* item =
An object contained in a set indicates that each element points to a set. If an element contains a set, it can be added to the set. Otherwise, it indicates that the set already contains.
* add ( * _set, * * = * element = > heap && < heap +* ==> heap && element < heap + (* element ==* ( *) element = -* element == - ( *
* find ( * _set, * * = * element = > heap && < heap +* ==> heap && element < heap +* * element == - heap ? ( *) element :
contains ( * _set, * find(_set, _element) !=
* drop ( * _set, * * element =* element =
differ ( * a, * a !=
The complete Set. c source code is as follows:
<Stdio. h> **! Defined threshold | threshold <1 limit 10 * (** p; (p = heap +; p