Data structure, data structure and Algorithm

Source: Internet
Author: User

Data structure, data structure and Algorithm
Linear table

A linear table is a typical linear structure. The basic feature is that the data elements in a linear table are ordered and limited. In this structure:

① There is a unique data element called "first;
② There is a unique data element called the "last;
③ Except the first element, each element has a unique direct precursor;
④ Except the last element, each element has a unique direct successor.

Linear table (Linear List): is composed of n (n Limit 0) data elements (nodes) a1, a2 ,... An finite sequence. All nodes in the sequence have the same data type. The specific meanings of data element ai in a linear table vary with specific applications. In the definition of a linear table, it is just an abstract representation. It can be a number, a string, or a record.
Logical Structure of a linear table

◆ A node in a linear table can be a single-value element (each element has only one data item ).
Example 1: An alphabet consisting of 26 English letters:
(A, B, C ,... , Z)
Example 2: the change of computer ownership of a school from 1978 to 1983 is given in the form of a linear table:
(6, 17, 28, 50, 92,188)
Example 3: points of a pair of poker games
(2, 3, 4 ,..., J, Q, K,)

The data elements in a linear table are various, but the same linear table must have the same characteristics, that is, it belongs to the same data object. There is a sequential relationship between adjacent data elements. Write a non-empty linear table as follows:
(A1, a2 ,... An)
A1 (NO precursor) is the first (first) node of a linear table,
An (no successor) is called the last (tail) node of a linear table.
When n = 0, it is called an empty table.
A1, a2 ,... Ai-1 is the precursor of ai (2 ≤ I ≤ n), where ai-1 is the direct precursor of ai;
Ai + 1, ai + 2 ,... An is the successor of ai (1 ≤ I ≤ N-1), where ai + 1 is the direct successor of ai.

Sequential storage structure of linear tables

##

Sequential storage: stores the nodes of a linear table in sequential logical order in a group of sequential address storage units. A linear table stored in this way is called a sequential table.

Features of a linear table stored in sequence:
◆ The logical sequence of a linear table is the same as that of a physical table;
◆ The relationship between data elements is reflected by the "physical location adjacent" of the elements in the computer.
In an advanced language (such as C Language) Environment: arrays have random access features. Therefore, arrays are used to describe sequential tables.
In addition to using arrays to store the elements of a linear table, a sequence table must have a Length attribute that represents a linear table. Therefore, the structure type is used to define the sequence table type.

#define  OK   1#define  ERROR   -1#define  MAX_SIZE  100typedef  int  Status ;typedef  int  ElemType ; typedef  struct  sqlist{   ElemType  *Elem_array ;int    length;} SqList ;
Basic operations on sequence tables
In the ordered storage structure, it is easy to perform operations on Linear tables: initialization, assignment, search, modification, insertion, deletion, and length determination.

Several main operations will be discussed below

Initialize an ordered linear table
Status Init_SqList (SqList * L) // create an empty table named L {L-> elem_array = (ElemType *) malloc (MAX_SIZE * sizeof (ElemType); if (! L-> elem_array) return ERROR; // If a null pointer is returned, the allocation fails. else {L-> length = 0; return OK ;}
Insert a node of the ordered linear table
In the linear table L = (a1 ,... A I-1, ai, ai + 1 ,..., An) Insert a new node e at the position I (1 ≦ I ≦ n + 1) to make it a linear table: L = (a1 ,... A I-1, e, ai, ai + 1 ,..., An)

If I = n + 1, the element is inserted to the last position of the table.
Steps
(1) Move the number I in the linear table L to the position after the nth node.
(2) Insert node e to the node ai-1.
(3) Add 1 to the length of a linear table.
Insert algorithm IDEA
If I = n + 1, insert x to the end of the table;

If the table length is n <0 or the insertion position is inappropriate, an error message is output and-1 is returned;

When 1 <= I <= n, a position is moved backward from the end of the table to the nth position (n-I + 1 data element must be moved together.

Finally, save e to L-> Elem_array [I]. The table length n is increased by 1, and the return value of the function is 1.
Algorithm Implementation

Status Insert_SqList (Sqlist * L, int I, ElemType e) {int j; if (I <1 | I> L-> length + 1) return ERROR; if (L-> length> = MAX_SIZE) {printf ("linear table overflow! \ N "); return ERROR;} for (j = L-> length-1; j> = I-1; -- j) l-> Elem_array [j + 1] = L-> Elem_array [j];/* Move all nodes after I-1 position */L-> Elem_array [I-1] = e; /* Insert the node at position I */L-> length ++; return OK ;}
Insert a new node at the I position in linear table L. The time is mainly spent on moving nodes in the Table. Therefore, the algorithm's time complexity can be estimated by moving nodes.

If I = 1, all n nodes need to be moved (worst: O (n ))
If I = n + 1 (inserted at the end of the table), no moving node is required. (Preferably O (1 ))
The probability of inserting a node before the I element in the linear table L is Pi without losing its universality. If the probability of inserting each position is equal, Pi = 1/(n + 1 ), the number of times the moving node is inserted is n-I + 1.

Delete an ordered linear table Node
In the linear table L = (a1 ,... A I-1, ai, ai + 1 ,..., An) delete node ai (1 segment I limit n) to make it a linear table: L = (a1 ,... Ai-1, ai + 1 ,..., An)

Steps
(1) Move the I + 1 to the n node in the linear table L to a forward position.
(2) The length of a linear table is reduced by 1.
Delete algorithm ideas
If the table length is n <= 0 or the deletion location is incorrect, an error message is output and-1 is returned;

If I = n, you only need to delete the end node, instead of moving the node;

When 1 <= I

The deletion algorithm implements ElemType Delete_SqList (Sqlist * L, int I) {int k; ElemType x; if (L-> length = 0) {printf ("linear table L is empty! \ N "); return ERROR;} else if (I <1 | I> L-> length) {printf (" the data element to be deleted does not exist! \ N "); return ERROR;} else {x = L-> Elem_array [I-1];/* Save the value of node I */for (k = I; k <= L-> length-1; k ++) L-> Elem_array [k-1] = L-> Elem_array [k]; /* forward all nodes after I + 1 */L-> length --; return (x );}}
Search, locate, and delete an ordered linear table

In the linear table L = (a1, a2 ,..., An) to delete the first node with the value of x.

Steps
(1) Search for the first data element whose value is x in linear table L.
(2) move one position forward from the found position to the last node.
(3) The length of a linear table is reduced by 1.

Algorithm Description

Status Locate_Delete_SqList (Sqlist * L, ElemType x)/* Delete the first node where the value of L in a linear table is x */{int I = 0, k; while (I <L-> length)/* Find the first node of x */{if (L-> Elem_array [I]! = X) I ++; else // find! {For (k = I + 1; k <L-> length; k + +) L-> Elem_array [k-1] = L-> Elem_array [k]; l-> length --; break; // jump out of the while LOOP} if (I> L-> length) {printf ("the data element to be deleted does not exist! \ N "); return ERROR;} return OK ;}
Time Complexity Analysis
The time is mainly spent on comparing and moving data elements.

First, check whether the node with the value of x exists in the linear table L;
Secondly, if a node with the value of x exists and its position in linear table L is I, the element I is deleted in linear table L.
In the linear table L, the probability of deleting data elements is Pi, without losing its universality. If the probability of each position is equal, Pi = 1/n.
◆ Average number of comparisons: Ecompare = Σ pi * I (1 ≦ I limit n)
Required Ecompare = (n + 1)/2.
◆ Average number of moves during deletion: Edelete = Σ pi * (n-I) (1 hour I bought n)
Receivedelete = (n-1)/2.
Average time complexity: Ecompare + Edelete = n, that is, O (n)

Related Article

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.