Read data structure (C) (4)

Source: Internet
Author: User

This section describes linear tables. This section describes the sequence Implementation of linear tables.

When it comes to linear tables, we may see arrays and linked lists in our minds. Yes, arrays and linked lists are both linear tables, but they are only two implementations of linear tables, emphasizing the physical structure of linear tables. When studying a data structure, we generally start with its logical structure, and wait for the study to clarify the logical structure before considering the specific physical implementation. Before writingProgramThe idea is the same. First, we need to identify which problems are logical and which are physical. First, the logic is followed by the general steps for solving computer problems. If you do not want to clearly understand the logic at first and stick it to the physical details, it is easy to clarify the idea or make a defective design. Of course, this is not absolute. In many cases, physical structures also affect the design of logical structures.

Simply put, a linear table is a finite sequence of n data elements with the same characteristics. Here, "n data elements with the same features" refer to data objects and "sequences" refer to data relations. Each data element has a definite position. Starting with the relationship between data objects and data, you can easily see the essence of a data structure. For a linear table, as long as some data has an order relationship and each element has the same characteristics, it can be considered as a linear table.

Next we will consider the sequential implementation of linear tables. In my own eyes, many people see that the sequential implementation of linear tables has been equal to the array. Although the sequential storage structure of linear tables is justified by the use of arrays, the sequential implementation should not be limited to arrays. If one day you have to use the assembly language to implement a linear table for sequential storage, you would not cry without an array. As mentioned by the author, the sequential representation of a linear table refers to storing data elements of a linear table in sequence using a set of sequential storage units with addresses. When implemented in C language, because linear tables have variable lengths and the maximum storage space required varies with the problem, we usually use a dynamically allocated one-dimensional array.

# Define list_init_size 100 // initial length
# Define listincrement 10 // increment allocation

Typedef struct {
Elemtype * ELEM; // storage base address
Int length; // The current length.
Int listsize; // storage capacity
} Sqlist;

When the above structure is used to allocate a bucket, malloc can be used for initial allocation, and realloc can be used when the space is not enough for redistribution. This function ensures that the content is not affected when the capacity is expanded based on the original allocation.

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.