Data Structure tutorial 18th class sequence representation and realization of array

Source: Internet
Author: User
Tags arrays definition exit

Teaching Purpose: to grasp the definition of array, the order of Array representation method

Teaching emphases: the definition of array, the order representation method of array

Teaching Difficulty: the sequential representation method of arrays

Teaching Content:

One, the definition of the array

Almost all programming languages set the array type to an intrinsic type.

Discussing the definition and implementation of arrays in the form of abstract data types allows us to deepen the understanding of the array types.

The definition of an array:

ADT array{

Data object: ji=0,..., bi-1,i=1,2,..., N;

D={aj1j2...jn|n (>0) is called the number of dimensions of an array, bi is the length of the array, and Ji is the first dimension subscript of the array element, AJ1J2...JN (-elemset}

Data relationship: R={R1,R2,... rn|

Ri={<aj1...ji...jn,aj1...ji+1 ... jn>|

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

0<=JI<=BI-2,AJ1...JI...JN,

Aj1...ji+1. JN (-d,i=2,... N}

Basic operations:

Initarray (&a,n,bound1,..., boundn)

If the dimension and the length of each dimension are valid, the corresponding array A is constructed and the OK is returned.

Destroyarray (&a)

Action Result: Destroy array A.

Value (a,&e,index1,..., indexn)

Initial conditions: A is an n-dimensional array, E is an element variable, followed by n subscript values.

Operation Result: If each subscript does not exceed bounds, then e assignment is the specified value of the element of a, and returns OK.

Assign (&a,e,index1,..., indexn)

Initial conditions: A is an n-dimensional array, E is an element variable, followed by n subscript values.

Operation Result: If the subscript does not exceed bounds, assigns the value of E to the element of the specified a, and returns OK.

}adt Array

One-dimensional array of column vectors:

A00 A01 A02 ... A0,n-1
A10 A11 A12 ... A1,n-1
... ... ... ... ...
am-1,0 am-1,1 am-1,2 ... Am-1,n-1

One-dimensional array of row vectors:

Each row in a two-dimensional array is considered to be a data element that consists of a one-dimensional array a.

A0
A00 A01 A02 ... A0,n-1
A10 A11 A12 ... A1,n-1
... ... ... ... ...
am-1,0 am-1,1 am-1,2 ... Am-1,n-1
A1
...
Am

Sequence representation and implementation of arrays

The way in which the rows are stored in the main sequence:

A00 A01 A02 ... A0,n-1 A10 A11 A12 ... A1,n-1 ... am-1,0 am-1,1 am-1,2 ... Am-1,n-1

Array in order to store representations and implementations:

#include <stdarg.h>

#define Max_array_dim 8

typedef struct {

Elemtype *base;

int Dim;

int *bounds;

int *constants;

}array;

Status Initarray (Array &a,int Dim,...);

Status Destroyarray (Array &a);

Status Value (Array a,elemtype &e,...);

Status Assign (Array &a,elemtype e,...);

The basic operation of the algorithm description:

Status Initarray (Array &a,int Dim,...) {

if (dim<1| | Dim>max_array_dim) return ERROR;

A.dim=dim;

a.bounds= (int *) malloc (Dim *sizeof (int));

if (! a.bounds) exit (OVERFLOW);

elemtotal=1;

Va_start (Ap,dim);

for (I=1;i<dim;++i) {

A.bounds[i]=va_arg (Ap,int);

if (a.bounds[i]<0) return underflow;

Elemtotal*=a.bounds[i];

}

Va_end (AP);

A.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]=1;

for (I=dim-2;i>=0;--i)

A.CONSTANTS[I]=A.BOUNDS[I+1]*A.CONSTANTS[I+1];

return OK;

}

Status Destoyarray (Array &a) {

if (! A.base) return ERROR;

Free (a.base); A.base=null;

if! (a.bounds) return ERROR;

Free (a.bounds); A.bounds=null;

if! (A.constatns) return ERROR;

Free (a.constants); A.constants=null;

return OK;

}

Status Locate (Array a,va_list ap,int &off) {

off=0;

for (I=0;i<a.dim;++i) {

Ind=va_arg (Ap,int);

if (ind<0| | Ind>=a.bounds[i]) return OVERFLOW;

Off+=a.constants[i]*ind;

}

return OK;

}

Status Value (Array a,elemtype &e,...) {

Va_start (ap,e);

if ((Result=locate (A,ap,off)) <=0 return result;

e=* (A.base+off);

return OK;

}

Status Assign (Array &a,elemtype e,...) {

Va_start (ap,e);

if ((Result=locate (A,ap,off)) <=0) return result;

* (A.base+off) =e;

return OK;

}

Third, summary

How the array is stored.

The basic operation type of the array.

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.