The order representation and realization of linear table

Source: Internet
Author: User
Tags array length

Linear table is a linear structure, we study its logical relationship, using ADT (abstract data type) to express, ADT Description can be expressed from the sequential structure and chain structure.

Representation and realization of linear table-------sequential structure

About sequential structures

Sequential structures are implemented and described by sequential tables. Sequential tables typically use one-dimensional arrays in the C language to represent sequential storage structures.

Sequential table structure features: Random lookup, delete insert trouble, variable size.

The data elements in a linear table are stored sequentially by a set of contiguous storage units, and each logically adjacent element in the sequential table is adjacent to the physical storage location. As the following illustration shows, A1,A2 is logically contiguous and the storage location in the physical table is adjacent.

Set each element to occupy a C storage unit, the relationship between the storage location of the i+1 element in the linear table loc (a i+1) and the storage location of the first element:

LOC (AI) = LOC (ai-1) + C

The storage of a data element ↑

Where all data elements are stored depends on where the first data element is stored

LOC (AI) = LOC (A1) + (I-1) XC

↑ Base Site

Description definition for sequential Image:

Note: In sequential structures, because of the characteristics of sequential structures (adjacent logically and physically), you can use pointers when defining and implementing sequential structures, because each element of a sequential structure is fixed according to the storage location and is one next to another. In order to understand this, we will use two methods to describe the sequential structure. One is using pointers, one is not using pointers.

Do not use    
pointers  #define  The allocation of MAXSIZE//linear table storage space, that is, the array length    
typedef  struct {        
    elemtype   elem[ MAXSIZE]; Defines the array    
    int  length;   The current length, the number of elements in the linear table, is not the array length    
} sqlist;  Commonly known    
as the sequence table pointer definition    
#define  MAXSIZE  //Linear table storage space allocation, that is, array length    
#define Listincrement    10//Linear table storage space allocation increment    
typedef  struct {        
    elemtype   *elem;//storage space Base Address    
int  length;   The current length, the number of elements in the linear table, is not an array length    
int  listsize;      Current allocated storage capacity    
} sqlist;  Commonly known as order table

In the above code, two methods are used to define the order table of a structure body, and the general name of unknown data type is represented by Elemtype.

SqList is the name of the table.

Note: The sequential definition of not using pointers defines a space that is immutable in length, that is, the size of the space cannot be changed, that is, the maxsize (100) element. Using a pointer definition defines a variable length space, which is a one-dimensional array of dynamic memory allocations, which increases the listincrement (10) space size based on the size of the original allocated space.

Sequential implementation of linear tables (initializing, finding, inserting, deleting, fetching elements)

There are two kinds of implementations of linear tables, one is sequential implementation and the other is chain implementation. In the above we have defined the sequential table sqlist, then we now first in order to implement.

A few examples of sequential implementations: note: In the above definition of sequential table sqlist, we used two methods (not pointers and pointers), then in the implementation of sequential structure, two methods are also implemented, corresponding to the two definitions respectively.

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.