Min "Data structure (c language Edition)"--Introduction to the 1th Chapter

Source: Internet
Author: User
Tags function prototype

data is a symbolic representation of an objective thing. In computer science, it refers to all the symbols that can be entered into a computer and processed by a computer program.

Data Elements are the basic unit of data, and are usually considered and processed as a whole in computer programs.

A data Object is a collection of data elements of the same nature and is a subset of the data.

A data structure is a collection of elements that have one or more specific relationships to each other.

A storage structure is a representation of a data structure in a computer.

A data type is a collection of values and a generic term that defines a set of operations on that set of values.

abstract data type refers to a mathematical model and a set of operations defined on the model, which is an extension of the generic data type.

The concept of data structure and abstract data type is different from that in programming language.

Solution: Abstract data types contain the concept of a generic data type, but the meaning is broader and more abstract than the general data type. The general data type is defined internally by the specific language system and is provided directly to the programmer to define user data. They are therefore called pre-defined data types. Abstract data types are typically defined by programmers, including defining the data that it uses and the operations that are performed on that data. When defining the data part and the operation part of the abstract data type, it is required to define only the logical structure and operation description of the data, regardless of the storage structure of the data and the concrete implementation of the operation, so that the abstraction level is higher and can provide good interface for other users.

The form of the data structure is defined as: The data structure is a two-tuple data_structure = (d,s) where D is a finite set of data elements, S is a finite set of relationships on D. (data, relationship)

Abstract data types can be represented by triples: (D,S,P) where D is a data object , S is the set of relationships on D, and P is the basic set of operations on D. (data, relationships, basic operations)

The definition format for an abstract data type:

  ADT Abstract data type name {

    Data Objects:< definition of Data Objects >

    data relationship:definition of < data relationship >

   Basic operation:

Basic operation name (parameter table)

        Initial conditions:< initial condition description >

        operation Result:< operation result description >

}ADT abstract data type name

The basic operation has two parameters: the assignment parameter only provides the input value for the operation;

The reference parameter starts with & and returns the result of the operation in addition to the input value.

"Initial conditions" describes the conditions that the data structure and parameters should meet before the operation executes, and if not, the operation fails and returns the corresponding error message.

The "Operation Result" describes the changes in the data structure and the results that should be returned when the operation is completed normally.

Abstract data type ternary group definition:

ADT Triplet {

Data object: D = {E1,e2,e3|e1,e2,e3∈eleset (a collection that defines a relational operation)}

Data relationship: R1 = {<e1,e2>,<e2,e3>}

Basic operation:

Inittriplet (&T,V1,V2,V3)

Operation Result: the ternary T, Element E1,e2 and E3 are respectively assigned the value of parameter v1,v2,v3.

Destorytriplet (&t)

Operation Result: Ternary group T is destroyed.

Get (T,i,&e)

Initial conditions: Ternary group T already exists, 1≤i≤3;

Operation Result: Returns the value of the element I of T with E.

Put (&t,i,e) (ternary group has been changed, return should be ternary T)

Initial conditions: Ternary t already exists.

Operation Result: Change the value of the I element of T to E.

Isascending (T)

Initial conditions: Ternary t already exists.

Operation Result: If the 3 elements of T are sorted in ascending order, 1 is returned, otherwise 0 is returned.

Isdesending (T)

Initial conditions: Ternary t already exists.

Operation Result: If the 3 elements of T are sorted in descending order, 1 is returned, otherwise 0 is returned.

Max (T,&e)

Initial conditions: Ternary t already exists.

Operation Result: Returns the maximum value in the 3 elements of T with E.

Min (T,&e)

Initial conditions: Ternary t already exists.

Operation Result: Returns the minimum value of the 3 elements of T with E.

} ADT Triplet

Representation and implementation of abstract data types:

(1) predefined constants and types:

#define TRUE 1

#define FALSE 0

#define OK 1

#define ERROR 0

#define INFEASIBLE-1 (not feasible)

#define OVERFLOW-2 (overflow)

Status is the type of function (return value) whose value is the function result status code

typedef int STATUS;

(2) Representation of a data structure (stored result) is described by a type definition (typedef), the data element type contract is Elemtype, which is defined by the user when using the data type.

(3) Generally, A, B, C, D, E, etc. are used as data element names,

I, J, K, M, n are used as Integer variable names,

P, Q, R, etc. are used as pointer variable names.

(4) Assignment statement:

Concatenation assignment variable Name 1 = variable name 2= = variable-name k= expression;

Group Assignment (variable name 1,, variable name k) = (expression 1,, expression k);

struct name = struct name;

struct name = (value 1,, value k);

Variable name []= expression;

Variable name [start subscript: Terminates subscript]= variable name [start subscript: termination of subscript];

Exchange assignment variable name ← → variable name;

(5) SELECT statement switch (expression) {

Case value 1: statement sequence; Break

......

Default: statement sequence;

}

          

switch{

Case Condition 1: statement sequence; Break

......

Default: statement sequence;

}

(6) Loop statement for while Do-while

(7) Concluding sentence function: return [expression];

Case:break;

Exception closing sentence: Exit (exception code);

(8) Input and output scanf ([format string], variable,..., n);

printf ([format string], expression 1,..., N);

(9) Insufficient integer value floor (expression);

Rounding integer value ceil (expression);

Decision file End EOF (file variable) or EOF

Decision Line End Eoln (file variable) or Eoln

(10) | | Operations and && Operations if expression 1 can determine the final result, then expression 2 is not executed.

The representation and implementation of the abstract data type triplet:

typedef elemtype * triplet;//is allocated 3 element storage space by Inittriplet//--------------Basic Operation function prototype description----------------Status Inittriplet ( Triplet &t, Elemtype v1, Elemtype v2, Elemtype v3);//Operation result: Constructs the ternary T, the element e1,e2 and the E3 values are assigned the value of the parameter V1,v2,v3 respectively. Status Destroytriplet (Triplet &t);//Operation result: Ternary T is destroyed. Status Get (Triplet t, int i, elemtype &e);//Initial condition: Ternary T already exists, 1<=i<=3. Operation Result: Return the value of the element I of T with E status Put (Triplet &t, int i, elemtype e);//Initial condition: Ternary T already exists, 1<=i<=3. Operation Result: Change the value of the I element of T to E. Status isascending (Triplet t);//Initial condition: ternary t already exists. Operation Result: If 3 elements of the ternary T are sorted in ascending order, 1 is returned, otherwise 0 is returned. Status isdescending (Triplet t);//Initial condition: ternary t already exists. Operation Result: If 3 elements of the ternary T are sorted in descending order, 1 is returned, otherwise 0 is returned. Status Max (Triplet T, Elemtype &e);//Initial condition: Ternary T already exists. Operation Result: Returns the maximum value in the 3 elements of T with E. Status Min (Triplet T, Elemtype &e);//Initial condition: Ternary T already exists. Operation Result: Returns the minimum value of 3 elements of T with E
Description of function prototypes--------------Basic operations----------------
Status Inittriplet (Triplet &t, Elemtype v1, Elemtype v2, Elemtype v3)
{//Operation result: The values of the ternary T, Element E1,e2 and E3 are assigned to the parameter v1,v2,v3 respectively. T = (Elemtype *) malloc (3 * sizeof (elemtype)); T) exit (OVERFLOW); Allocate storage space failed t[0] = v1; T[1] = v2; T[2] = V3;return OK;} Inittripletstatus Destroytriplet (Triplet &t) {//Operation result: Ternary T is destroyed. Free (T); T = Null;return OK;} Destroytripletstatus Get (Triplet t, int i, Elemtype &e) {//use E to return the value of the element I of Tif (i<1 | | i>3) Return error;e = T[i-1];return OK;} GetStatus Put (Triplet &t, int i, elemtype e) {//1<=i<=3. Change the value of the I element of T to E. if (i<1 | | i>3) return ERROR; T[i-1] = E;return OK;} Putstatus isascending (Triplet t) {//If the 3 elements of the ternary T are sorted in ascending order, 1 is returned, otherwise 0 is returned. Return (T[0] >= t[1]) && (t[1] >= t[2]);} Isascendingstatus isdescending (Triplet t) {//If the 3 elements of the ternary T are sorted in descending order, 1 is returned, otherwise 0 is returned. Return (T[0] <= t[1]) && (t[1] <= t[2]);} Isdescendingstatus Max (Triplet T, Elemtype &e) {//uses E to return the maximum value in the 3 elements of T. E = (t[0] >= t[1])? ((T[0] >= t[2])? T[0]:T[2]): ((t[1] >= t[2])? T[1]:T[2]); return OK;} Maxstatus min (Triplet t, Elemtype &e) {//use E to return the minimum value in the 3 elements of T. E = (t[0] <= t[1])? ((T[0] <= t[2])? T[0]:T[2]): ((t[1] <= t[2])? T[1]:T[2]); return OK;} Min

Min "Data structure (c language Edition)"--Introduction to the 1th Chapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.