Linear table of data structure and algorithm

Source: Internet
Author: User
Tags disk defragmenter

Objective

The concept of time complexity and the common time complexity are introduced in the time complexity and space complexity of data structure and algorithm, and one by one explanations are given respectively. This article mainly introduces the linear table.

Linear table belongs to the linear structure in the logical structure of data structure. Recall that the data structure is divided into physical structure and logical structure, the logical structure is divided into linear structure, geometric structure, tree structure and graphic structure of the four major structures. Linear table is a linear structure. The remaining three logical structures will be introduced in the future.

Basic concepts of linear tables

Linear table (list): A finite sequence consisting of 0 or more data elements.

Attention:

1. A linear table is a sequence.

A linear table consisting of 2 elements is an empty table.

3. The first element in a linear table has no precursors, the last element has no successor, and the other elements have only one precursor and successor.

4. The linear table is of length, its length is the number of elements, and the number of elements of the linear table is limited, that is, the length of the linear table is limited.

If you are using a mathematical language to define, you can do the following:

If the linear memento is (A1,..., ai-1,ai,ai+1,... An), the ai-1 in the table is ahead of Ai,ai, which is called ai+1 is the direct precursor element of AI, Ai-1 is the direct successor of AI.

Basic operation of Linear table

Initlist(*l): Initializes the operation to create an empty linear table L.

Listempty(L): Determines whether the linear table is a null table, returns TRUE if the linear table is empty, otherwise returns false.

Clearlist(*l): empties the linear table. Getelem(l, I, *e): Returns the value of the first position element in the linear table L to E.

< Span class= "sy0" >< Span class= "br0" >< Span class= "sy0" >locateelem (L,e ) : Finds an element in linear table L equal to the given value E, returns the element in the table if the lookup succeeds; otherwise, returns 0 indicates a failure.

< Span class= "sy0" >< Span class= "br0" >< Span class= "sy0" >< Span class= "sy0" >listinsert (*l,i,e) : Inserts a new element e in the linear table L at the first position.

< Span class= "sy0" >< Span class= "br0" >< Span class= "sy0" >< Span class= "sy0" >< Span class= "br0" >listdelete (*l,i,*e) : Remove the I-position element in linear table L and return its value with E.

listlength (L: Returns the number of elements of the linear table L.

For different applications, the basic operation of the linear table is different, the above operation is the most basic.

For more complex operations on linear tables involved in real-world problems, it is entirely possible to use a combination of these basic operations.

Two different kinds of linear tables

We know that the data structure is divided into logical structure and physical structure, and the logical structure is divided into four categories: set structure, linear structure, tree structure and graphic structure. The physical structure is divided into sequential storage structure and chained storage structure. I've already covered the data structures and algorithms that I wrote earlier.

Linear table is one kind of linear structure, so the linear table also has the physical structure, that is to say, the linear table has two kinds, namely the linear table of the sequential structure (called the sequential table) and the Chain structure linear table (called the linked list).

1. Linear table of sequential storage structures

A sequential table is a linear table of sequential storage structures, which refers to the data elements of a linear table stored sequentially by a storage unit with successive addresses.

The Sequential table table is now in physical memory, that is, physical storage, in fact, to find an initial address in memory, and then through the form of placeholder, a certain amount of memory space to occupy, and then the same data type of data elements are placed in this space. Note that the address space for this physical memory is contiguous.

For example, such as the storage of basic variables in C language is stored in memory continuously, such as declaring an integer I, in 64-bit system integer i in memory 8 bytes, then the system will be in memory for this integer variable is allocated a length of 8 bytes of contiguous address space, The binary form of this I is then stored from the high address to the low address, when the length is insufficient, the highest bit is 0.

struct-body definition for sequential tables
#define MAXSIZE  int elemtype;  //Order table stored data type struct// linear table current length} sqlist;    

By defining the order table above with the struct, we can see that the package of the sequential table requires three attributes:

1. The starting position of the storage space. the storage location of the array data is the storage location of the linear table storage space2. Maximum storage capacity of the linear table. Array length MAXSIZE3. The current length of the linear table. LengthNote: The length of the array is not the same as the current length of the linear table. The length of the array is the total length of the storage space in which the linear table is stored, usually unchanged after initialization. The current length of the linear table is the number of elements in the linear table, which can be changed. Sequential Table lookup element Operations

Code implementation:

Sequential Table Insert element operation

Ideas are as follows:

1. If the insertion position is unreasonable, throw an exception;

2. If the linear table length is greater than or equal to the array length, throw an exception or dynamically increase the array capacity;

3. Move forward from the last element to the I position, moving each of them backward one position;

4. Fill in position I with the insertion element;

5. Linear table Length +1.

Code implementation:

Sequential Table Delete element operation

Ideas are as follows:

1. Throws an exception if the location of the deleted element is unreasonable. For example, the user deletes the No. 0 position element (the linear table is starting from 1), the location of the deleted element is greater than the length of the linear table and throws an exception. 2. Remove the element from position I. 3. Add one to the position of all elements behind the element in position I. 4. Linear table length minus one. Code implementation:advantages and disadvantages of sequential tables

As you can see from the above code:

Sequential storage structure of linear tables, where the time complexity is O (1), regardless of the location in which the data is stored or read. In the case of insertions or deletions, the time complexity is O (n). This is also the sequential storage structure of the linear table is more suitable for accessing data, and is not suitable for applications that frequently insert and delete data. Pros: 1. There is no need to add additional storage space (as opposed to chained storage) in order to represent a logical relationship between elements in a table. 2. You can quickly access elements from anywhere in the table. Cons: 1. Insert and delete operations require a large number of elements to be moved. 2. When the linear table length varies greatly, it is difficult to determine the capacity of the storage space. 3. The "fragmentation" of the storage space (because the sequential storage structure of the linear table requests the memory space to be contiguous, if there is a small block of discontinuous memory space due to some operation (such as a delete operation), because this small amount of memory space is too small to be used/allocated again, The ps:windows system has a Disk Defragmenter tool, and the Linux system does not, because the Linux system kernel is optimized for very good, almost no disk fragmentation. 2. Linear table of chained storage structure

The main drawback of the sequential storage structure of the linear table that we talked about earlier is that it takes time to move a large number of elements when inserting and deleting.

Can we propose a solution to this flaw, or regret? To solve this problem, we have to consider the cause of this problem!

Why do we move a large number of elements when inserting and deleting?

The reason is that the adjacent two elements of the storage location also has a neighbor relationship, their location in memory is next to each other, there is no gap between, of course, can not be quickly inserted and deleted.

The chain storage structure of linear tables is characterized by the use of a set of arbitrary storage units to store data elements of linear tables, which can exist anywhere in memory that is not occupied.

In other words, the linear table of a chained storage structure consists of one (which can make 0) or multiple nodes (node). Within each node are data fields and pointer domains (chains). The data field stores information about the data element. The pointer field stores the direct successor pointer address that the current node points to. Because each node contains only one pointer field, it is called a single-linked list. As the name implies, there are, of course, doubly linked lists. Single linked list

In a chained storage structure, in addition to storing data element information, it stores the storage address (pointer) of its successor elements.

This means that in addition to storing its own information, it is necessary to store a storage location indicating its direct successor.

We call the domain where data element information is stored, and the field where the direct successor is stored is called the pointer field.

The information stored in a pointer field is called a pointer or chain.

These two pieces of information comprise a data element called a storage image, or node.

n nodes are linked into a linked list, which is a chain storage structure for linear tables (A1, A2, A3, ..., an).

Because each node in this list contains only one pointer field, it is called a single-linked list.

For linear tables, there is always a tail, and the list is no exception. We call the storage location of the first node in the list called the head pointer, and the last node pointer is empty (NULL).

Single-linked list is the most representative of the linear table, the next article, I will take out a chapter to introduce a single-linked list, please look forward to!

Photo source reference from: Fish C Studio. Thanks to the Fish C studio for contributing such a good picture.
If not specifically described, the author of all articles are original articles. If you like this article, reprint please indicate the source. If you are interested in data structure, please pay attention to me, follow-up will update a large number of excellent articles for your reference!

PS: This article in the book also has a synchronous update, we can also step into the book to focus on himself, follow-up will update more articles!

Linear table of data structure and algorithm

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.