Data Structure Order table, insert, delete, etc.

Source: Internet
Author: User
Tags printf

In the first half of the year to put more energy on the specialized courses, has not allotted much time to pay attention to other interests, when you take the initiative to learn knowledge, it becomes more interesting, into the topic, these days began to learn the data structure, mainly as follows:

A data structure focuses on three aspects of the content:

1 Logical structure of data: the logical relationship between exponential elements

It is divided into two major types of a linear structure: the classical linear structure is a linear table, the data element in the linear structure has 1 to 1 relationship

b Nonlinear Structure: The classical nonlinear structure is the tree structure and the graphic structure, there are 1-to-many hierarchies among the elements in the tree structure, and there are many-to-many network structures among the elements in the graph structure.

2 storage structure of data: refers to the logical structure of data to the mapping of computer memory,

There are several ways to implement the structure of data storage in common use

A sequential storage

The basic idea is that logically adjacent data elements are stored in the adjacent storage unit in the physical location, and the logical relationship between the data elements is represented by the adjacency relation of the storage unit.

B-Chain storage: The basic idea is to represent the relationship between data elements by attaching a pointer field, which does not require logically adjacent data elements to be stored in the same location, and the logical relationship between the data elements is obtained by attaching an address information (pointer) indicating the location of other data elements.

C Index Storage:

The basic idea is that in addition to storing data elements, one or several additional index tables are created to identify the address of the data element, and each item in the index table is called an index entry and is used to identify the storage location of one or a set of data elements

D Hash Storage

The basic idea is that the storage address of the node is calculated directly according to the key word of the data element, often called the keyword-address translation method.

3 operation of data: refers to the processing and processing of data elements

Differences and similarities between the two algorithms and programs

(1) The method and procedure for solving a particular problem in an algorithm is an ordered sequence with several instructions.

The algorithm needs to satisfy the poor, for any set of legitimate input values, the algorithm must be executed after the poor steps to end, a limited time to complete, but the program can not meet the requirements of poor

(2) The instructions in the program must be machine executable, but the instructions in the algorithm do not have this limitation

The similarities:

An algorithm that is written in a machine-readable language is a program.

Quantitative evaluation of three pairs of algorithms

(1) Time complexity, which is the system time that an algorithm runs, that is, the time efficiency of an algorithm (the time complexity of the algorithm is determined by the frequency of the inner loop of the nested loop)

(2) Space complexity, which is the storage space consumed by the algorithm when it is run, i.e. the space efficiency of the algorithm

Note that, in general, both can not be combined, to save the execution time of the algorithm is often at the expense of storage space

And more system time is necessary to save storage space.

The four-linear table is the most basic and most commonly used data structure, what is the linear table?

A linear table (Linear list) is an ordered sequence of n (n>=0) data elements that have the same attribute, the number of data elements n is the length of a linear table, when n=0 is called an empty table, that is, the table contains no elements, and typically a non-empty linear table (N>=0) is written as:

L= (a1,a2,a3,,ai-1,ai,ai+1, An

The logical characteristics of a linear table are the same as the logical characteristics of a linear structure: there is only one starting node and one terminal node, and the remaining internal nodes have only one predecessor and one successor.

The five linear tables are stored sequentially as sequential tables, and linear tables are stored in chained form as linked lists.

Six the sequential table storage structure is described in C as follows:

#define MAXSIZE 1024

typedef int DATATYPE;

typedef struct

{

DataType Data[maxsize];

int last;//, the actual number of elements in a linear table should be less than or equal to maxsize, so a variable is required to record the subscript of the last element in the array in the current linear table, with the previous variable

}seqlist;

Seqlist SL;

Insert operation of seven linear tables

The insertion of a linear table is the insertion of a new element with a value of x in the form I (1<=i<=n+1) of table L, the table with the original length n inserted

(a1,a2,a3,,ai-1,ai,ai+1, An

Become a table of length n+1

(a1,a2,a3,,ai-1,x,ai,ai+1, An

The idea is:

In general, inserting an element in the position I (1<=i<=n) requires that a position be moved backwards from the nth element to the I element and then to the n+1,n,,i+1, and then the new element x is inserted into the first position.

Before writing the C function of the implementation algorithm, the main steps in the algorithm should be analyzed first

Step 1: Because it is an insert operation, first to check whether there is an insertion position, that is, the order table is full, if the order table is full, an overflow error, return insert failed

Step 2: In the presence of the insertion position, but also to check whether the insertion position is appropriate, that is, if the 1<=i<=n+1 is satisfied, if the condition is not met, the insertion position is wrong, return insert failure

Step 3: Implement a post-move operation on the premise that the steps are successfully passed

Step 4: Insert a new node

Step 5: Add the table length plus 1 to return the Insert success

The C function of the insertion algorithm is as follows:

Int Insertelem (seqlist *l,int I, DataType x)

{

Int J;

If (l->last==maxsize-1)

{

Printf ("Overflow");

Return 0;

}

If ((i<1) | | | (i>l->last+2))

{

Printf ("error");

Return 0;

}

for (j=l->last;j>=i-1;j--)

l->data[i+1]=l->data[j];

l->data[i-1]=x;

l->last++;

Return 1;

}

Note: The valid range for the insertion position is 1<=i<=n+1, but when n+1<i<maxsize, there are reserved storage units in the order table, and there can be insert operations. The answer is no, because there is no guaranteed logical relationship between elements

Deletion of eight linear tables

Removes the 1<=i<=n element from the linear table and removes the original table length by one

The idea is:

In general, if you delete the element I (1<=i<=n-1), you need to move the first i+1 to the nth element in turn, moving forward one

The C function of the delete algorithm is as follows

Int Deleteelem (seqlist *l,int i)

{

Int J;

If (i<1| | I>L->LAST+1)

{

Printf ("%d element does not exist", I);

Return 0;

}

for (j=i;j<=l->last;j++)

l->data[j-1]=l->data[j];

l->last--;

Return 1;//indicates successful deletion

}

Search by value for nine linear tables

is to find data elements that are equal to a given x in a linear table,

The idea is:

From the first element and x, if an element equal to X is found, it returns the ordinal in the linear table, indicating that the lookup succeeds, if the entire table is not found and the x equals element, returns 0, indicating that the lookup failed

The corresponding C language functions are as follows:

Int Locatelem (seqlist *l,datatype x)

{

Int i;

for (i=0;i<=l->last;i++)

If (l->data[i]==x)

Return i+1;//returns the ordinal number

Return 0;

}

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.