Introduction trilinear table data structure - sequential storage

Linear table is like?

 

 

 Such as this, can be used as assembly line work life is the same.

Linear table is n (N> = 0) of data elements (nodes), such as a1, a2, a3 .... an finite sequence consisting of

N defines the number of data elements for the length of the table,

When n = 0, the table is empty (null) 

 

When n> 0 is a non-empty list L = (a1 .... an)

a1 called starting point, an end of the node.

For any neighbor node pile ai and ai + 1 (1 <i <n) ai ai + 1 is the direct precursor, ai + 1 is a direct successor of ai

 

 

 (Photo from Internet)

If the linear form as it becomes more apparent the train, the train is only one locomotive (start node) a rear (terminal node)

Locomotive (start node) is not in front of the cabin (direct precursor) but behind a further carriage (direct successor), rear (terminal nodes) behind no extra rear (direct successor), but there is one in front of the rear compartment (direct precursor).

Summing can understand that each node has a direct and immediate predecessor and a successor

 

Logical Structure of the linear form

For linear non-empty table: linear table node has one to one relationship (see specifically train the example above)

1) in a linear form in fact there is only one node a1, the precursor is not directly, and only a direct successor A2;

2) there is only one terminal node and an, there is no direct successor, and some only a direct precursor an-1;

3) the remaining internal node ai (a <= i <= n-1) and only has a direct precursor ai-1, and a direct successor ai + 1;

 

The basic operation of the linear form

 

Initiate Initialization autumn table length Length get taken bucket positioning insert Insert Delete Delete Locate

 

Linear table stored in the order is: the junction of the table are sequentially stored in computer memory in a group of contiguous memory locations , the abutting relationship of data elements in a linearization table determines which storage locations in a storage space, i.e., a logical structure adjacent nodes storing location is adjacent.

With the storage order to achieve the linear form is referred to as sequence table. It is generally used an array to a sequence table. 

Assuming a set of data, there is data between the sequence: 1,345,159,532,450,884,211

I.e., between the sequence data here represents the logical relationship between data that is a linear relationship, this set of data to the linear form:

 

 

 Granted the LOC address a1 (a1), each of the data units representing c, is calculated address ai

Place (ai) = Rank (a1) + c * (i-1)

For example a3 address

1 a1 = 0  
2  
3 Position (a3) = a1 + ( 3 - 1 )
 4  
5              = 0 + 2 
6  
7              = 2

initialization

strudt student{
int ID;//ID
char name [30]; // Name
char sex; // Sex
int class; // class
int age; // Age

}
student={"01",“zhangsan”,"m","201","20"};

  insert

The insertion operation of the linear form in the i refers to Table (1 <= i <= n + 1) th position, insert a new node x, n is the length of the linear form ": (a1, .... ai-1, ai, ai + 1, .... an)

Into a length of n + 1 linear table:

(A1, ... ai-1 x , ai, ai + 1, .... an)

1) When the table is full, do not insert

2) When the insertion position of the illegal position, do not normal insertion operation

 

 

 

void   InsertSeqlist (SeqList L, X dataType, int I) {
 IF (l.length == Maxsize) Exit ( " table is full " );
 IF (I < . 1 || + l.length . 1 ) Exit ( "Position Error") ; // inserted in the correct position 
for (J = l.length; J> = I; J -) { // initialization = l.length I 
l.data [J] = l.data [J- . 1 ]; / / turn backward 
l.data [I- 1 ] = x; // element x is inserted into the index i-1 position 
l.length ++;} // table length plus 1

假设线性表中含有n个数据元素,
在进行插入操作时,有 n+1个位置可插入
在每个位置插入数据的概率是:1/(n+1)
在i位置插入时,要移动n-i+1 个数据

平均时间复杂度为O(n)

 

删除

线性表的删除运算是指将表的i的结点删去,使长度为n的线性表长度”减一“

当要删除元素的位置I不在表长范围内(即i<1或i->length)时,为非法位置,不能做正常的删除操作

操作过程:

1,若i=n,则只要删除终端结点,无需移动结点;

2,若1<=i<=n-1,则必须将表中位置i+1,i+2,,,,,n的结点,依次前移到位置I

3,该表长度减1

void DeleteSeqList(SeqList L,int i) {
//删除线性表L中的第i个数据结点
if(i<1 || i>L.length) //检查位置是否合法
exit(“非法位置”);
for(j=i;j<L.length;j ++) //第i个元素的下标为i-1
L.data[j-1]=L.data[j]; //依次左移
L.length--; //表长度减1
}

 删除算法的分析

 

假设线性表中含有n个数据元素,
在进行删除操作时,有 n位置可删除
在每个位置删除数据的概率是:1/n
在i位置删除时,要移动 n-i+1个数据
假定在n个位置上删除元素的可能性均等,

平均时间复杂度为O(n)顺序存储结构表示的线性表,在做插入或删除操作时,平均需要移动大约一半的数据元素。

当线性表的数据元素量较大,并且经常要对其做插入或删除操作时,这一点需要值得考虑 

定位(查找)

定位运算LocateSeqlist(L,X)的功能是
求L中值等于X的结点序号的最小值,

当不存在这种结点时结果为0 从第一个元素 a1 起依次和x比较,直到找到一个与x相等的数据元素,则返回它在顺序表中的存储下标或序号;或者查遍整个表都没有找到与 x 相等的元素,返回0 

int LocateSeqlist(SeqList L, DataType x)
{
int i=0;
while ((i<L. length) && (L.data[i]!=x) ) //在顺序表中查找值为 x 的结点
i++;
if(i<L.length) return i+1; //若找到值为x的元素,返回元素的序号
else return 0; //未查找到值为x的元素,返回0
}
//顺序表的求表长操作,直接输出L.length即可

  1)设表的长度length=n,在插入算法中,元素的移动次数不仅与顺序表的长度
n有关, 还与插入的位置i有关。 插入算法在最坏情况下,其时间复杂度为O(n)
一般情况下元素比较和移动的次数为n-i+1次,插入算法的平均移动次数约为n/2
其时间复杂度是O(n)
2)删除算法DeleteSeqlist,可得其在最坏情况下元素移动次数为n-1,时间复杂
度为O(n),元素平均移动次数约为(n-1/2,时间复杂度为O(n)
3)对于定位算法,需要扫描顺序表中的元素。以参数x与表中结点值的比较为
标准操作,平均时间复杂度为O(n)。求表长和读表元素算法的时间复杂度为O(1)
就阶数而言,己达到最低

顺序表的优点:

无需为表示结点间的逻辑关系而增加额外存储空间
可以方便地随机存取表中的任一结点

顺序表的缺点:

插入和删除运算不方便,必须移动大量的结点
顺序表要求占用连续的空间,存储分配只能预先进
行,因此当表长变化较大时,难以确定合适的存储
规模

Guess you like

Origin www.cnblogs.com/X404/p/12013976.html