Data structure and algorithm 2-1 linear sequential storage

Source: Internet
Author: User

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

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.