Linear table of data structure (1)

Because of preparing for postgraduate entrance examination, I want to do in-depth study of data structure, and prepare to write a blog to record the knowledge learned. If there are any problems, please point them out.


First definition: A linear table is a finite sequence of data elements with the same characteristics.

There are two storage structures of linear table: sequential storage structure and chain storage structure. The first is called a sequential list, and the second is called a linked list. (For the memory of both structures, a sequential list is like an array, and a linked list is like a train in life.)

Comparison of storage structures

1. The sequence table requires continuous storage space;

2. The linked list supports the dynamic allocation of storage space, and also supports applying for a continuous storage space; (at the beginning of contact, the illusion that the linked list can only dynamically allocate storage space)

3. The static linked list is represented by an array, which requires a large continuous space.

Advantages and disadvantages of sequential list and linked list structure:

1. The sequence table can directly access the elements in the table, while the linked list needs to access the position elements through the traversal of the nodes.

2. The time complexity of adding and deleting elements in a sequential list is higher, while that in a linked list is lower.

Explanation: Because in addition to adding or deleting the last element in the sequence table, it is necessary to cover the deleted element by moving the element. When adding an element, it is necessary to move the element to empty the inserted position. Linked lists add and remove elements by mutating pointers, all it takes is to find where to operate.

Let's record the code and operation ideas of the basic operation part of the linear table.

First is the sequence table

typedef struct array{
    int *data;
    int size,length;
}array;

The structure definition of the sequence table is as shown above, *data represents the data field of the table, size represents the size of the table, which is declared during init, and length represents the current length of the table.

The init operation performs memory allocation for the pointer, the size is initialized to the given size, and the length is initialized to 0.

The insertion operation first determines whether the insertion position is legal, and returns 0 if it is illegal; if it is legal, determine whether the length of the sequence table has reached the maximum, if it is, expand the sequence table, and then move all the elements after the insertion position by one position. The position is set to the inserted value, and the length is incremented by one after insertion.

The deletion operation is also to determine whether the position is legal, and the moving element covers the element at the target position. It should be noted that the movement should start from the first element to the right of the deletion position, not the last element of the sequence list.

The printing table operation is to traverse the entire table and output one by one.

If the whole program is written, the memory of the sequence table should be released at the end of the main function.

free(array->data);
free(array);

First, release the pointer field of the sequence table, and then release the sequence table.


This article records the basic operations of the sequence table, and the next article records the basic operations of the linked list and some concepts.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324724684&siteId=291194637