Linear table sequential structure, non-chained:
Consists of two parts:
1. The data is stored in the array , and the maximum capacity is given at the same time;
2. The subscript of the last data, which suggests the total number of data actually stored.
Structure Description:
typedef struct() { double Data[max];//maximum number of stores, max capacity of the table int last;//last Is the last subscript that actually stores the data; }List; List *Ptrl; Ptrl->last+1 is the total number of data stored, that is, the actual length of the table
Data table effects, note The +1 relationship between the number of points and the subscript, avoiding code errors:
Create an empty table function, note that the request for memory requires a cast , otherwise the structure of a null pointer
List *Makeempty () { list *Ptrl; Ptrl= (list *) malloc (sizeof (list)); ptrl->last=-1;//empty table return Ptrl; }
Find Data functions
int find (Double x,list *Ptrl) { int i=0; while (ptrl->data[i]!=x && i<=ptrl->) i++; if (i>ptrl->last) return-1; else return i;}
Insert function, insert 1<=i<=last+2 data, that is, can be inserted in the position of subscript 0--last+1:
Double X,int I, List *Ptrl) { int j; if (ptrl->last==max-1) {printf ("full"); return;} The table already has Max Data if (i<1 | | i>ptrl->last+2) {printf ("error"); return;} can only insert 1th count to last+2 number for (j=ptrl->last; j>=i-1; j--) ptrl->data[j+1]=ptrl->Last[j] ; ptrl->data[i-1]=x; ptrl->last++;//store actual data number plus 1, subscript plus 1, Note Update the total number of data return
Delete the function, delete the I (1<=i<=last+1) data, that is, you can delete the subscript 0-last data
void Delete (int i,list *ptrl) { int j; if (ptrl->last==-1) {printf ("empty"); return;} if (i<1| | i>ptrl->last+1) {printf ("error"); return;} for (j=i-1;j<ptrl->last;j++) ptrl->data[j]=ptrl->data[j+1]; ptrl->last--;//actual number of data stored-1, subscript-1, note Update total data return;
Data structure and algorithm 2-1 linear sequential storage