Type definition of linear table

Source: Internet
Author: User

Introduction to Linear Tables

A linear structure is a set of ordered (sequential) data elements.

The basic characteristics of a linear structure are:

1. There must be only one "first element" in the collection;

2. There must be a unique "last element" in the collection;

3. With the exception of the last element, there is only one successor;

4. Except for the first element, there is only one precursor.

Type definition of linear table

ADT (abstract data type) is the description of the logical structure, its implementation is implemented with physical storage, there are two kinds: sequential storage structure and chain storage structure.

The abstract data type linear table is defined as follows:

ADT list{data object: d={ai | ai∈elemset, i=1,2,..., N, n≥0} {The table length (also the number of elements) called N is a linear table, and the linear table called n=0 is an empty table. Data relationships: r1={<ai-1, Ai >|ai-1, ai∈d, i=2,..., n} i is a bit-ordered basic operation: Initialize->initlist (& L) {construct-Empty linear table L} Destroy->destroy (&l) {destroy} is an empty table->listempty (l) {if L return true} null->listlength (l) {return        
The number of elements in L, that is, the table length} takes the precursor->priorelem (l,cur_e,&pre_e) {Cur_e as an element and is not the first, then returns its predecessor with Pre_e} To the successor->nextelem (l,cur_e,&next_e) takes element->getelem (l,i,&e) {Returns the value of the first element in L with E) to locate->locateel                                     
EM (L,e,compare ()) {Returns the sequence of elements in L that satisfies compare () with E, otherwise returns 0} Traverse->listtraverse (L,visit ()) {sequentially invoke the visit () function} for each element of L ()} null->clearlist (&l) {empty} to change element-> Putelem (&l,i,e) {overlay E to position I, is to change element, not insert} Insert->listinsert (&l,i,e) {Insert position is front of i} Delete->listdelete ( &L,i,&)}ADT List 

Example 1: Set A and B are represented by two linear tables, LA and LB respectively, that is, the data element in the linear table is the member of the collection. A new set of a=a∪b is required

Analysis: Expands the linear table LA to insert data elements that exist in linear table lb without being present in the linear table LA

To the linear table in LA.

void Union (List &la, list LB) {    
    La_len = listlength (LA);    To find the length of the linear table    
Lb_len = Listlength (LB);     
for (i = 1;  I <= Lb_len;  i++) {    getelem (LB, I, e);//Take the first data element of LB to assign to E    if (! Locateelem (LA, E, equal ())  //e compared to LA elements, if not, insert       listinsert (LA, ++la_len, E);//LA does not exist the same data element as E, then insert the    
}    
}                                            O (Listlength (La) xlistlength (Lb))      

Example 2: The data elements in the linear table LA and LB are ordered in a non descending order of values, and it is required that LA and lb be merged into a new linear table LC, and that the data in the LC is still ordered in descending order by value.

Analysis: The LC is first set to an empty table, and then the elements in LA or lb are inserted into the LC one by one. You can set two integer variables I, J, respectively, point to LA and lb, compare the size of the elements I and J refer to, and decide which element to insert into the LC. After insertion, in order to move back in LA or lb

void Mergelist (list LA, list LB, list &lc) {    
   initlist (LC);  Structured null linear table LC    
   i = j = 1;    k = 0;     
   La_len = Listlength (LA);    
   Lb_len = Listlength (LB);    
   while ((I <= La_len) && (J <= Lc_len)) {       //la and lb are not null    
        Getelem (LA,I,AI); Getelem (LB,J,BJ);    
        if (AI<BJ) {    Listinsert (LC, ++k, AI);  ++i   }     
        else{  listinsert (LC, ++k, BJ);  ++j   }       
  }    
   while (I<=la_len) {   //if LA not empty    
        Getelem (La,i++,ai);           
        Listinsert (LC, ++k, AI);       
   } Insert the remaining elements in the La Table while        
   (J<=lb_len)  {  //If LB is not empty    
        Getelem (LB,J++,BJ);           
        Listinsert (LC, ++k, BJ);     
   } Insert the remaining elements in the Lb table    
}//Merge_list    
                                              O (listlength 2 (La) +listlength 2 (Lb))

This article is from the "Zhao Yuqiang blog" blog, please be sure to keep this source http://zhaoyuqiang.blog.51cto.com/6328846/1160126

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.