Data structure linear table sequential storage structure and main algorithm implementation

(1) The definition of linear table.

A finite sequence of zero or more data elements
Insert picture description here

There are direct successor elements in the sequence linear table, some and only one direct successor, and some and only one direct predecessor. The relationship between data elements is a one-to-one relationship

Common List operations:

Operation

InitList(*L)://初始化操作,简历一个空的线性表L
ListEmpty(L)://若线性表为空,返回true,否则返回Falser
GetElme(L,i,e)://将线性表L中的第i个位置元素值返回给e
LocateElme(L,e)://在线性表中L中查找与给定值e相等的元素,如果查找成功,返回该元素在表中的序号表示成功;否则,返回0表示失败
ListInsert(*L,i,e)://在线性表L中的第i个位置插入新元素e
ListDelete(*L,i,*e)://删除线性表中L中第i个位置元素,并用e返回其值
ListLength(L)://返回线性表L的元素个数

(2) Linear

The sequential storage structure of the table and the implementation of main algorithms, such as search, insertion, and deletion algorithms.

Definition: The sequential storage structure of a linear table refers to storing the data elements of the linear table at a time with a section of storage units with consecutive addresses
Insert picture description here

Sequential storage method: Occupy the memory space in the form of occupancy, and then store the data elements of the same data type in it in turn. A one-dimensional array realizes the sequential storage structure

#define MAXSIZE 20  /*储存空间初始分配量*/
typedef int ElemType;/*ElemType类型根据实际情况而定,假设为int*/
typedef struct {
    
    
ElemType data[MAXSIZE]/*据存储数据元素,最大值为MAXSIZE*/
int length;  /*线性表当前长度*/
}
sqlist;

*The starting position of the storage space: array data, its storage location is the storage location of the storage space

*Maximum storage capacity of linear table: array length MaxSize

*The current length of the linear table: length

Insertion and deletion of sequential storage structure:

Get element operation: To implement the GetElem operation is to return the value of the i-th position element in the linear table

#define OK1
#define ERROR 0
#define TRUE 1
#define FALSE 
typedef int status;
/*status是函数的数据类型,其值是函数结构状态代码,如OK等*/
/*初始条件:顺序线性表L一存在,1<=i<=ListLength(L)*/
/*操作结构:用e返回L中第i个数据元素的值*/

Linear table sequence storage structure insertion operation:

The insertion operation of the linear table refers to inserting a new data element n before the i-th data element of the linear table

The idea of ​​the insertion algorithm:

The time complexity of the insertion algorithm:

Insert picture description here

The derivation of the time complexity shows that the average time complexity of the insertion operation is O(n).

*If the insertion position is not reasonable, throw an exception

*If the length of the linear table is greater than or equal to the length of the array, an exception is thrown or the capacity is dynamically increased

*From the last element, traverse forward to the i-th position, and move them all one position backward respectively

*Fill the element to be inserted into position i out

*The length of the table plus 1

/*初始条件:顺序线性表L以存在1<=i<=ListLength(L),*/
/*操作结构:在L中第i个位置之前插入新的数据元素e,L的长度加1*/
status ListInsert(Sqlist *L,int i,ELemType e){
    
    
int k;
if(L->length==MAXSIZE)/*顺序线性表已经满*/
return ERROR;
if(i<=L->length)/*若插入数据位置不在表尾*/
{
    
    
for(k=L->length-1;k>=i-1;k--/*将要插入位置后数据元素向后移动一位*/
L->data[k+1]=L->data[k];
}L->data{
    
    i-1]=e;/将新元素插入*/
L->length++;
return OK;
}

The deletion of the sequential storage structure of the linear table: the deletion of the linear table refers to the deletion of the i-th data element (0<=i<=n-1) in the table, so that the linear table with length n becomes length n-1 Linear table of, need to move the i+1th to n-1th element one position forward

The idea of ​​the deletion algorithm:

Time complexity of the deletion algorithm:
Insert picture description here

The derivation of the time complexity shows that the average time complexity of the delete operation is also O(n).

*If the delete position is not reasonable, throw an exception

*Remove and delete elements

*From the deleted element position to the last element position, so do not move them all forward one position

*Table length minus 1

The characteristics of the sequential storage structure of the linear table:

advantage:

*Disordered to express the logical relationship between the elements in the table and add extra storage space

*You can quickly access elements at any position in the table

Disadvantages:

* Insert and delete operations need to move a lot of elements

*When the length of the linear table changes greatly, it is difficult to determine the capacity of the storage space

* Causes "fragmentation" of storage space

Guess you like

Origin blog.csdn.net/weixin_45743004/article/details/103303450