Data structure-linear table sequential storage structure

Source: Internet
Author: User

Linear table
 线性表是一种典型的线性结构。其基本特点是线性表中的数据元素是有序且是有限的。在这种结构中:

① There is a unique data element known as the "first";
② There is a unique data element called the "last";
③ In addition to the first element, each element has a single direct precursor;
④ except for the last element, each element has a single direct successor.

   线性表(Linear List) :是由n(n≧0)个数据元素(结点)a1,a2, …an组成的有限序列。该序列中的所有结点具有相同的数据类型。线性表中的数据元素 ai 所代表的具体含义随具体应用的不同而不同。在线性表的定义中,只不过是一个抽象的表示符号,它可以是一个数字、一个字符串或一个记录。
Linear table Logical Structure

A node in a linear table can be a single-valued element (only one data item per element).
Example 1:26 an alphabet consisting of English letters:
(A,b,c 、...、 Z)
Example 2: A school from 1978 to 1983 various models of computer ownership changes, in the form of a linear table is given:
(6,17,28,50,92,188)
Example 3: A pair of poker points
(2,3,4,...,j,q,k,a)

The data elements in a linear table are various, but the same linear table must have the same characteristics, that is, the same data object. There is a sequence-couple relationship between adjacent data elements. The non-empty linear memento is used as:
(a1,a2,...an)
A1 (no precursor) is called the first (first) node of a linear table,
An (no successor) is called the Last (tail) node of the linear table.
When N=0, it is called an empty table.
A1,a2,...ai-1 are the precursor of AI (2≤i≤n), in which ai-1 is the direct precursor of AI;
Ai+1,ai+2,...an are the successor of AI (1≤i≤n-1), in which Ai+1 is the direct successor of AI.

Sequential storage structure of linear tables

##

    顺序存储 :把线性表的结点按逻辑顺序依次存放在一组地址连续的存储单元里。用这种方法存储的线性表简称顺序表。

The characteristics of sequential stored linear tables:
The logical order of the linear table is consistent with the physical order;
The relationship between data elements is reflected in the "physical location adjacent to the element" in the computer.
In high-level languages such as the C language: arrays have random access characteristics, so arrays are used to describe sequential tables.
In addition to using arrays to store elements of a linear table, the sequential table should have a length attribute that represents the linear table, so a struct type is used to define the sequential 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 for sequential tables
   顺序存储结构中,很容易实现线性表的一些操作:初始化、赋值、查找、修改、插入、删除、求长度等。

Several major operations are discussed below.

Sequential linear table Initialization
*//新建一张名为L的空表{  L->elem_array=* )malloc(MAX_SIZE*sizeof( ElemType ) ) ;if!-> return  //如果返回空指针,则表示分配失败else {   L->length=0 ;    return OK ;  }  
Insertion of sequential linear table nodes
 在线性表 L= (a1,…a i-1,ai, ai+1,…,an) 中的第i(1≦i≦n+1)个位置上插入一个新结点e,使其成为线性表:  

If i=n+1, the element is inserted at the last position of the table
Implementation steps
(1) Move the line I to nth node in the linear table L to one position after the other.
(2) Insert the node E after the junction ai-1.
(3) Linear table length plus 1.
Insertion Algorithm Idea
If i=n+1, the X is inserted at the end of the table;

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

When 1<=i<=n, a position is moved backward from the end of the table to the first I (the n-i+1 data element must be moved in a total.)

Finally, E is deposited in l->elem_array[i], the table length n increases by 1, the function return value is 1.
Algorithm implementation

Status insert_sqlist (SqList *l,intI, Elemtype e) {intJ;if(i<1|| I>l->length+1)returnERROR;if(l->length>=max_size) {printf ("Linear table overflow!\n");returnERROR; } for(j = l->length-1; J >=i-1; --J) l->elem_array[j+1]=l->elem_array[j];/ * All nodes after the i-1 position are moved * /l->elem_array[I-1]=e;/ * Insert a node in the i position * /L->length++ ;returnOK; }
   在线性表L中的第i个位置插入新结点,其时间主要耗费在表中结点的移动操作上,因此,可用结点的移动来估计算法的时间复杂度。

If i=1, all n nodes need to be moved (worst: O (n)).
If i=n+1 (inserted at the end of the table), there is no need to move the node. (Preferably O (1))
The probability of inserting a node before the first element of the linear table L is pi, without losing its generality, the probability of inserting each position is equal, then pi=1/(n+1), and the number of times the node is moved when inserted is n-i+1.

Deletion of sequential linear table nodes
在线性表 L=(a1,…a i-1,ai, ai+1,…,an) 中删除结点ai(1≦i≦n),使其成为线性表:       L= (a1,…ai-1,ai+1,…,an)

Implementation steps
(1) The i+1 to the nth node in the linear table L moves forward one position in turn.
(2) Linear table length minus 1.
Delete Algorithm Ideas
If the table length n<=0 or deletion position is not appropriate, then output error message, and return-1;

If i=n, just delete the tail node, do not move the node;

When 1<=i

Delete Algorithm implementation Elemtype Delete_sqlist (sqlist*l,inti) {intK Elemtypex;if(l->length==0){printf("Linear table L is empty!\n");returnERROR; }Else if(i<1|| I>l->length) {printf("The data element to be deleted does not exist!\n");returnERROR; }Else{x=l->elem_array[i-1] ;/ * Save the value of the first node * / for(K=i; K<=l->length-1; k++) l->elem_array[k-1]=l->elem_array[k];/ * All nodes after i+1 position move forward * /L->length--;return(x);}}
Find-and-locate deletion of sequential linear table

In linear table l= (a1,a2,...,an), delete the first node with the value x.

Implementation steps
(1) Look for the first Data element with the value x in the Linear table L.
(2) Move one position forward from the found position to the last node in turn.
(3) Linear table length minus 1.

Algorithm description

Status locate_delete_sqlist (sqlist *l,elemtype x)/ * Delete the first node of the linear table L with a value of X * /{intI=0K while(i<l->length)/ * Find the first node with a value 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 while loop}}if(i>l->length{printf ("The data element to be deleted does not exist!\n");returnERROR; }returnOK; }
Analysis of time complexity
时间主要耗费在数据元素的比较和移动操作上。

First, the presence of a node with a value of x in the linear table L is found;
Second, if a node with a value of x exists and the position in the linear table L is I, then the element i is removed from the linear table L.
Set in linear table L Delete data element probability is pi, without losing generality, set each position is equal probability, then pi=1/n.
Average number of comparisons: Ecompare=∑pi*i (1≦i≦n)
∴ecompare= (n+1)/2.
Average number of moves when deleted: edelete=∑pi* (N-i) (1≦i≦n)
∴edelete= (n-1)/2.
Average time complexity: Ecompare+edelete=n, which is O (n)

Data structure-linear table sequential storage structure

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.