Linear table & Sequential Linear table

Source: Internet
Author: User
Chapter II Linear table

References: [Data structure (C language version)]. Min
This chapter is for personal study of data structure notes, not for any purpose.

2.1 Characteristics of the linear structure

(1). There is only one data element called "first"
(2). There is only one data element known as the "last"
(3). Except for the first, each data element in the collection has a unique precursor
(4). Except for the last one, each data element in the collection has a unique successor

2.2 Type definitions for linear tables

linear tables (linear_list) are the simplest and most commonly used data structures. A linear table is a finite sequence of n data elements.

For example, the alphabet of 26 English letters:

(A,b,c,..., Z)

The above is a linear table, and the data element is a single alphabetic character.

For example, the age of each family member in a family can be given in the form of a linear table:

(6,17,32,38,48,70,96)

The data elements in the linear table above are integers.

The complex linear table situation is as follows:

    • A data element consists of several data items (item) .
    • In the case above, the data element is called the record.
    • A linear table that contains a large number of records is called a file.

For example, a student's Student health status registration form. The situation of each student in the table is a record .
It consists of 5 data items , such as name, school number, gender, age, class, etc.

name School Number Sex Age class
Zhang Xingdong 790302 Man 28 ITCSC 062
Lu Ye 790303 Woman 26 ITCSC 061

Therefore, the abstract description of the linear table is as follows:

\[(A_{1},..., a_{i-1},a_{i},a_{i+1},..., a_{n}) \]

When i=1,2,..., n-1, Ai has and has only one direct successor.
When i=2,3,..., N, Ai has and has only one direct precursor.
The number n (n>=0) of elements in a linear table is defined as the length of a linear table, and n=0 is an empty table.
Each data element in a non-empty table has a definite location. such as A1 is the first Data element, such as an is the last data element, the AI is the first element, I for the data element AI in the linear table of the bit order.

Structure of the abstract data type:

ADT 抽象数据类型名{   数据对象:<数据对象的定义>   数据关系:<数据关系的定义>   基本操作:<基本操作的定义>} ADT 抽象数据类型名

The definition of a linear table of abstract data types:

ADT list{Data object: D ={a (i) |a (i) belongs to Elemset, i=1,2,..., n,n>=0} data relations: R ={<a (I-1), A (i) >|<a (i-1), a (i) belongs to d,i=2, ...,      n} Basic Operation: Initlist (&AMP;L) operation result: Constructs an empty linear table L.         Destorylist (&l) Initial conditions: Linear table L already exists.      Operation Result: Destroy Linear table L.         Clearlist (&l) Initial conditions: Linear table L already exists.      Operation Result: Resets L to an empty table.         Listempty (l) Initial conditions: Linear table L already exists.      Operation Result: Determine if L is an empty table, if yes returns True, not return false.         Listlength (l) Initial conditions: Linear table L already exists.      Operation Result: Returns the number of data elements in L.      Getelem (l,i,&e) Initial conditions: Linear table L already exists, 1<=i<=listlength (l) Operation result: Use E to return the value of the I data element in L.         Locateelem (L,e,compare ()) Initial conditions: Linear table L exists, compare () is a data element determination function. Operation Result: Returns the bit order of the first Data element in L that satisfies the relationship compare () with E. If such a data element does not exist, the return value is 0.      (in layman's terms, the location of the returned data element)         Priorelem (l,cur_e,&pre_e) Initial conditions: Linear table L already exists. Operation Result: If Cur_e is the data element of L, and is not the first one, then return its precursor with pre_e, otherwise the operation fails, Pre_e does not exist.      (in layman's terms, the precursor of the returned data element)         Nextelem (l,cur_e,&next_e) Initial conditions: Linear table L already exists. Operation Result: If Cur_e is the data element of L, and is not the last one, then returns its successor with Next_e, otherwise the operation fails, Next_e does not exist.  (in layman's terms, returns the successor of the data Element)    Listinert (&l,i,e) Initial conditions: Linear table L already exists, 1<=i<=listlength (l) +1.      Operation Result: Inserts a new data element in the L position before the e,l length plus 1.         Listdelete (&l,i,&e) Initial conditions: Linear table L already exists, 1<=i<=listlength (L).      Operation Result: Delete the I data element in L, and return its value with E, the length of L minus 1.         Listtraverse (L,visit ()) Initial conditions: Linear table L already exists. Operation Result: Call visit () for each data element of L in turn.                Once visit () fails, the operation fails. (in layman's terms, each data element is traversed)} ADT List
2.3 Sequential representation and implementation of linear tables

The sequential representation of a linear table refers to the data elements of a linear table that are stored sequentially with a contiguous set of storage cells .
It is assumed that each element of a linear table needs an L storage unit and that the storage address of the first cell is the storage location of the data element. The storage location of the i+1 data element, loc (a1+1) and the first Data element, loc (AI) satisfies the following relationship:

\[LOC (A_{i+1}) =loc (A_{i}) +l\]
In general, the first data element in a linear table is stored in the following location:
\[LOC (A_{i}) =loc (A_{1}) + (i-1) *l\]

This expression is called the sequential structure of a linear table. Also known as sequential images (sequential mapping). Its features are:
Two adjacent data elements ai and a1+1. The "physical location" of its storage is also adjacent. As follows:

Since the starting position of the storage linear table is determined, each data element is stored in a constant proportional to the starting position of the linear table, and the data element is in direct proportion to the order of the linear table, and any data element in the linear table can be randomly accessed, so the sequential structure of the linear table is a random access storage structure.

The C language can dynamically allocate a one-dimensional array to describe the sequential storage structure of a linear table, where the array pointer elem indicates the base address of the linear table, and length represents the current length of the linear table, as follows:

// - - - - - 线性表的动态分配顺序存储结构 - - - - -#define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量#define LISTINCREMENT 10   //线性表存储空间的分配增量typedef  struct {    ELemType   * elem;     //存储空间基址    int        length;     //当前长度    int        listsize;   //当前分配的存储容量(以sizeof(ElemTpye)为单位)}SqList;

The following algorithm is used to construct an empty linear table. The initialization operation is to assign a predefined size array space to the sequential table and set the current length of the linear table to "0". Listsize is the size of the storage space currently allocated.

Status  InitList_Sq(SqList &L){    //构造一个空的线性表L。    L.elem = (ELemType *)malloc(LIST_INIT_SIZE * sizeof(ELemType));    if(! L.elem) exit(OVERFLOW);    //存储分配失败    L.length = 0;                   //空表长度为0    L.listsize = LIST_INIT_SIZE;    //初始化存储容量    return OK;}// InitList_Sq
Sequential storage of linear tables insert operations:

For example, inserting a new data element between the I-1 data element and the I data element in a linear table makes a linear table of length n.

\[(A_{1},..., a_{i-1},a_{i},..., a_{n}) \]
into a linear table of length n+1
\[(A_{1},..., a_{i-1},b,a_{i},..., a_{n}) \]

In a sequential storage structure in a linear table, the logically adjacent data elements are also adjacent in the physical location. Therefore, the data element is inserted, and the storage location changes as follows.

Insertion algorithm for sequential linear tables:

Status  ListInsert_Sq(SqList &L,int i,ElemType e){    //在顺序线性表中L中第i个位置之前插入新的元素e。    //i的合法值为1<=i<=ListLength_Sq(L)+1    if(i < 1 || i >L.length+1) return ERROR;  //i值不合法    if(L.length >L.listsize){        newbase = (ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT) * sizeof(ElemTpye));        if(! newbase)exit(OVERFLOW);          //存储分配失败        L.elem = newbase;                     //新基址        L.listsize += LISTINCREMENT;          //增加存储容量           }    q = &(L.elem[i-1]);                       //q为插入位置    for (p = &(L.elem[L.length-1]);p >= q; --p)           *(p+1) =*p;                         //插入位置及之后的元素后移    * q =e;                                   //插入e    ++L.length;                               //线性表长度+1    return OK;}// ListInsert_Sq
Sequential storage of linear tables Delete operations:

For example, deleting a data element in a linear table makes a linear table of length n.

\[(A_{1},..., a_{i-1},a_{i},a_{i+1}...,a_{n}) \]
into a linear table of length n-1
\[(A_{1},..., a_{i-1},a_{i+1},..., a_{n}) \]

The data element is deleted and the storage location changes as follows.

Delete algorithm for sequential linear table:

Status  ListDelete_Sq(SqList &L,int i,ElemType &e){    //在顺序线性表中L中删除第i个元素,并用e返回其值。    //i的合法值为1<=i<=ListLength_Sq(L)    if(i < 1 || i >L.length+1) return ERROR;  //i值不合法    p = &(L.elem[i-1]);                       //q为删除元素的位置    e = *p;                                   //被删除元素的值赋予e    q = L.elem+L.length-1;                    //表尾元素的位置    for(++p;p<=q,++p) *(p-1) = *p;            //被删除元素之后的位置左移    --L.length;                               //线性表长度-1    return OK;}// ListDelete_Sq
Sequential store lookup operations for linear tables:

Lookup algorithms for sequential linear tables:

int LocateElem_Sq(SqList L,ElemType e,Status (*compare)(ElemType,ElemType)){    //在顺序线性表中L中查找第一个与e满足compare()的元素的位序。    //若找到,则返回其在L中的位序,否则返回0。    i = 1                                     //i的初值为第一个元素的位序。    p = L.elem;                               //p的初值为第一个元素的存储位置。    while(i<=L.length && !(*compare)(* p++,e)) ++i;    if(i<=L.length) return i;    else return 0;}// LocateElem_Sq
Sequential storage of linear tables for merge operations:
  void mergelist_sq (sqlist la,sqlist lb,sqlist &lc) {///known order Linear table La and LB elements by value non-descending arrangement//merge L A and LB get the new sequential linear table LC,LC elements also by the value of non-descending permutation of PA =la.elem;       Pb=lb.elem;       Lc.listsize =lc.length = la.length+lb.length;       PC = Lc.elem = (Elemtpye *) malloc (lc.listsize *sizeof (Elemtpye)); if (! Lc.elem) exit (OVERFLOW);       Storage allocation Failure Pa_last = La.elem + la.length-1;       Pb_last = Lb.elem + lb.length-1;          while (PA <= pa_last && pb <= pb_last) {//merge if (*pa <= *pb) *pc++ = *pa++;       else *pc++ = *pb++;       } while (PA <= pa_last) *pc++ = *pa++; while (PB <= pb_last) *pc++ = *pb++;} MERGELIST_SQ  

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.