The fifth chapter: 1. Arrays and generalized tables--arrays

Source: Internet
Author: User

Objective:

The data elements in the linear structure discussed in chapters 2, 3, and 4 are non-structural atomic types, and the values of the elements are no longer decomposed. The two types of data structures discussed in this chapter---arrays and generalized tables can be thought of as linear tables in the following extensions: The data elements in the table are themselves also a data structure.

The array is a well-known data type, almost all programming languages set the array type to the intrinsic type, the first two are in the form of abstract data types to discuss the definition and implementation of the array, so that readers deepen the understanding of the logarithm group.

Directory:

1. Definition of arrays

2. Sequential representation and implementation of arrays

3. Compression storage of the matrix

4. Definition of generalized tables

5. Storage structure of generalized tables

Representation of a 6.m-tuple polynomial

7. Recursive algorithm for generalized tables

Body:

First, the definition of the array

Similar to a linear table, an array of abstract data types can be defined in the form:

ADT array{

Data Objects:

Ji=0, Bi-1, I=1,2,...,n

D={aj1 J2 ... jn | n (>0) is called the dimension of the array, and BI is the length of the I dimension of the array, Ji is the i-dimensional subscript of the array element, Aj1 J2 ... jn∈elmeset}

Data relations:

R={r1,r2 ... Rn}

ri={

< Aj1 ... ji ... jn, aj1 ... ji+1 ... jn > |

0<= JK <=bk-1,1<= k <=n and K <> i

0<= ji <=bk-2,

Aj1 ... ji ... jn, aj1 ... ji+1 ... jn∈d, i = 2, ... n

}

Basic operation:

Initarray ();

Destoryarray ();

Value ();

Assign ();

}adt Array;

Second, the order of the array expression and implementation

Since arrays are generally not inserted and deleted, that is, once an array is established, the relationship between the data elements and elements in the structure is no longer changed. Therefore, it is natural to use sequential storage structures to represent arrays.

Since the storage unit is a one-dimensional structure, and the array is a multidimensional structure, there is a sequential problem in storing the data elements of the array with a contiguous set of storage cells.

For example, for a two-dimensional array, it can be considered as a one-dimensional array, and each array element of one-dimensional arrays is a one-dimensional array. There are two types of storage for a two-dimensional array:

1. Sequence-based storage

2. The method of storing the main sequence of the line sequence

      

Thus, for an array, once you have defined the length of his dimension and dimensions, you can allocate storage space for it. Conversely, a set of subscripts can be used to find the storage location of the corresponding array elements.

Most programming languages use a sequence-based approach to storage. The following is illustrated by the example of a storage method based on the sequence of rows

Assuming that each data element occupies an L storage unit, the storage location of any element aij in a two-dimensional array A can be determined as follows:

LOC (I, j) = loc (0,0) + (B2 * I +j) *l

LOC (I, j) is the storage location for a IJ, and LOC (0,0) is the storage location of the a00, that is, the starting storage location of the two-dimensional array A, also known as the base address.

It is generalized to the equation where the data elements of an n-dimensional array are stored:

LOC (J1, ... jn) = loc (0, ... 0) + (b2*b3*...*bn*j1+ b3*b4*...*bn*j2+ ... + bn*jn-1 + JN) * L

=loc (0, ... 0) +∑ci ji where cn=l, ci-1 = bi * ci,1< i <n

LOC (0, ... 0) +∑ci Ji is called an image function of an n-dimensional array. Once the length of each dimension of the array is determined, CI is a constant.

Sequential storage structure for arrays:

struct {    *base;                 // array element Base address, assigned    by Initarray int Dim;                        // number of    array dimensions int *bounds;                    // array dimension base address, assigned    by Initarray int *constants;                 // array image function constant base address, assigned by Initarray (CONSTANTS[I]=CI)} Array;

Code implementation:

#include <stdio.h>#include<stdlib.h>#include<stdarg.h>//the Stdarg.h header file defines a variable type va_list and three macros,//These three macros can be used to get the arguments in the function when the number of arguments is unknown (that is, the number of arguments is variable). #defineTRUE 1#defineFALSE 0#defineOK 1#defineERROR 0#defineINFEASIBLE-1#defineOVERFLOW-2#defineUNDERFLOW-3#defineMax_array_dim 8//Status is the type of function whose value is the function result status codetypedefintStatus;typedefCharElemtype;typedefstruct{elemtype*Base;//array element Base address, assigned by Initarray    intDim//number of array dimensions    int*bounds;//array dimension base address, assigned by Initarray    int*constants;//array image function constant base address, assigned by Initarray (CONSTANTS[I]=CI)}array;//Initialize array A]Status Initarray (Array &a,intDim,...) {    if(dim<1|| Dim>Max_array_dim)returnERROR; A.dim=Dim; A.bounds=(int*)malloc(dim*sizeof(int)); if(!a.bounds) exit (OVERFLOW); //initializes the length of each dimension and calculates the total number of elements    intElemtotal=1;    Va_list ap;    Va_start (AP, Dim);  for(intI=0; i<dim;i++) {A.bounds[i]=va_arg (AP,int);//receive an indeterminate parameter after the Dim parameter in turn        if(a.bounds[i]<0)returnunderflow; Elemtotal*= A.bounds[i];//Total count of elements (multiplicative dimension)} va_end (AP); //allocate all storage space for dim-dimensional Array aA.Base= (Elemtype *)malloc(elemtotal*sizeof(Elemtype)); if(! A.Base) exit (OVERFLOW); A.constants=(int*)malloc(dim*sizeof(int)); if(!a.constants) exit (OVERFLOW); A.constants[dim-1]=sizeof(Elemtype);  for(intj=dim-2; j>=0; j--) {A.constants[j]=a.bounds[j+1]*a.constants[j+1]; }        returnOK;} Status Destoryarray (Array&A) {    //Destroy Array a    if(! A.Base)returnERROR;  FreeA.Base); A.Base=NULL; if(! A.bounds)returnERROR;  Free(A.bounds); A.bounds=NULL; if(! a.constants)returnERROR;  Free(a.constants); A.constants=NULL; returnOK;}//calculates the relative position of the element at a and returns it with offStatus Locatearray (Array &a,va_list AP,int&off) {Off=0;  for(intI=0; i<a.dim;i++){        intLocate=va_arg (AP,int); if(locate<0|| Locate>A.bounds[i])returnOVERFLOW; Off+=a.constants[i]*Locate; }    returnOK; }//returns a value that corresponds to a valid subscript. Status Value (Array &a,elemtype *e,...)    {va_list ap;    Va_start (ap,e); intoff;    Locatearray (A,ap,off);        Va_end (AP); *e=* (A.Base+off); returnOK;}//Assigning a value to a valid subscript for a arrayStatus Assign (Array &a,elemtype e,...)    {va_list ap;    Va_start (AP, E); intoff;    Locatearray (A,ap,off);        Va_end (AP); * (A.Base+off) =e; returnOK;}voidMain () {Array A; Initarray (A,4,3,4,5,6); //Assign ValueAssign (A,'b',2,1,4,1);    Elemtype e; //Take valueValue (A,&e,2,1,4,1); printf ("value:%c\n", E);}

The fifth chapter: 1. Arrays and generalized tables--arrays

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.