Data structure - sequence table of linear table

linear table

1. Linear table and its logical structure

1. Definition of linear table

A linear list is a finite sequence of data elements with the same properties.

  • Limited: The number of data elements is limited.
  • Sequence: Data elements are uniquely identified by a logical sequence number.
  • Same property: All elements belong to the same data type.
    The number of elements contained in the linear table is called the length of the linear table, represented by n, n>=0. When n=0, it means that the linear table is an empty table, that is, the table does not contain any elements.
    The logic expression of the linear list is as follows:
    insert image description here
    insert image description here
    9 basic operations of the linear list are as follows:
    1. Initialize the linear list InitList(&L): Construct an empty linear list L.
    2. Destroy the linear list DestroyList(&L): Release the memory space occupied by the linear list L.
    3. Determine whether the linear list is an empty list ListEmpty(L): If L is an empty list, return true, otherwise return false.
    4. Find the length of the linear list ListLength(L): returns the number n of elements in L.
    5. Output linear table DispList(L): When the linear table L is not empty, the value ranges of the nodes in L are displayed sequentially.
    6. Find a data element at the specified position of the linear table L GetElem(L,i,&e): use e to return the value of the i-th (1<=i<=n) element in L.
    7. Locate search LocateElem(L, e): returns the logical bit sequence of the first value range in L equal to e. If no such element exists, the return value is 0.
    8. Insert an element ListInsert(&L, i, e): Insert a new element e before the i (1<=i<=n) element of L, and increase the length of L by 1.
    9. Delete the data element ListDelete(&L,i,&e): delete the i (1<=i<=n) element of L, and return its value with e, and the length of L is reduced by 1.

2. The role of the linear table

insert image description here

  • Programmers can use it directly to store data - as a container for storing data.
  • Programmers can directly use its basic operations to complete more complex functions.
    insert image description here

2. Sequential storage of linear tables - sequential tables

The sequential storage structure is to store all the elements in the linear table according to the sequential storage method.
It is stored in a continuous storage space in the memory in a logical order.
insert image description here

1. Sequence table type definition

typedef struct
{
    
    
ElemType data[MaxSize];
int length;
};SqList;//顺序表类型

Assume ElemType is char type.
Among them, the data member stores the elements, and the length member stores the actual length of the linear table.
Note: The difference between logical bit sequence and physical bit sequence is 1.

2. Realization of sequence table operation

1. Create a sequence table
a[0…n-1] ⇒ \Rightarrow⇒Sequence table L——to create a sequence table as a whole.

void CreateList(SqList *&L,ElemType a[],int n)	//L表示顺序表中的指针
{
    
    
int i=0,k=0;
L=(SqList *)malloc(sizeof(SqList));
while(i<n)	//i扫描a中元素
{
    
    
L->data[k]=a[i];
k++;	//k记录插入到L中的元素个数
i++;
}
L->length=k;
}

in,
insert image description here

Basic Operational Algorithm of Sequence Table

(1) Initialize the linear list InitList(L)

Constructs an empty linear list L. Actually just set the length member to 0.

void InitList(SqList *&L)
{
    
    
L=(SqList *)malloc(sizeof(SqList));	//分配存放线性表的顺序表空间
L->length=0;
}

(2) Destroy the linear table DestoryList(L)

Release the storage space occupied by the linear table L.

void DestroyList(SqList *&L)
{
    
    
free(L);	//释放L所指向的内存空间
}

Advantages of using pointer passing in sequence table:

  • See sequence table creation and destruction process (malloc/free) more clearly.
  • It is more space-efficient to pass between functions of the algorithm (no need to create value parameters in the function body.

(3) Determine whether it is an empty list ListEmpty(L)

Returns a value indicating whether L is an empty list. Returns true if L is an empty list, otherwise returns false.

bool ListEmpty(SqList *L)
{
    
    
return(L->length==0);

(4) Find the length of the linear table ListLength(L)

Returns the length of the sequence list L. In fact, you only need to return the value of the length member.

int ListLength(SqList *L)
{
    
    
return(L->length);
}

(5) Output linear table DispList(L)

When the linear list is not empty, sequentially display the values ​​of elements in L.

void DispList(SqList *L)
{
    
    
	int i;
	if(ListEmpty(L))
	return;
	for(i=0;L->length;i++)
	printf("%c",L->data[i]);
	printf("\n");
}

(6) Find a data element value GetElem(L,i,e)

Returns the value of the i (1<=i<=ListLength(L)) element in L and stores it in e.

bool GetElem(SqList *L,int i,ElemType &e)
{
    
    
	if(i<1||i>L->length)
	return false;
	e=L->data[i-1];
	return true;
	}

The time complexity of this algorithm is 0(1). Embodies the random access characteristics of the sequence table.

(7) Find LocateElem(L,e) by element value

Sequentially finds the logical order of the first element whose value is equal to e, and returns 0 if such an element does not exist.

int LocateElem(SqList *L,ElemType e)
{
    
    
	int i=0;
	while(i<L->length&&L->data[i]!=e)
	i++;
	if(i>=L-length)
	return 0;
	else return i+1;
}

(8) Insert data elements ListInsert(L,i,e)

Insert a new element e at position i (1<=i<=ListLength(L)+1) of sequence list L.
insert image description here
The algorithm is as follows:

bool ListInsert(SqList *&L,int i,ElemType e)
{
    
    
	int j;
	if(i<1||i>L->Length+1)
	return false;	//参数错误时返回false
	i--;	//将顺序表逻辑序号转化为物理序号
	for(j=L->Length;j>i;j--)	//将data[i...n]元素后移一位
	L->data[j]=L->data[j-1];
	L->data[i]=e;	//插入元素e
	L->length++;	//顺序表长度增加1
	return ture;	//成功插入返回ture
}

The insertion method is shown in the figure:
insert image description here
For this algorithm:

  • Best: when i=n+1, that is, new elements are inserted at the end of the list, and the number of moves is 0; (the best time complexity is 0(1))
  • Worst: When i=1, that is, a new element is inserted into the header, and the number of moves is n, reaching the maximum value. (worst time complexity 0(n))
  • Average case: Assume that the probability of inserting a new element into any position is the same, that is, the probability of i=1, 2, 3...length is p=1/n; then i=1, loop n-1 times, i=2, loop n-2 times...i=n, loop 0 times.
    Then the average number of cycles=n(-1)p+(n-2)p+...+1*p=n/2.

(9) Delete data elements ListDelete(L,i,e)

Delete the i (1<=i<=ListLengh(L))th element in the sequence list L.

bool ListDelete(SqList *&L,int i,ElemType &e)
{
    
    
	int j;
	if(i<1||i>L->length)
	return false;	//此处作用为卡i的范围,判断i的值是否合法
	i--;	//执行这一步是因为数组都是从0开始取的,这样可以将逻辑位序转换化成物理位序
	e=L->data[i];
	for(j=1;j<L->length-1;j++)
	L->data[j]=L->data[j+1];	//将数据表的每个元素前移一位
	L->length--;	//顺序表长度减少一位
	return true;	//删除成功
}

Explanation: The reason why & is used in the declared function here is that the purpose of L is to make it point to the same address in this function and the main function, and the purpose of e is to make it correspond to the same data in the main function and this function.

For this algorithm, the number of element moves is also related to the position of the deleted element

  • Best time complexity: when i=n, ​​move 0 times; 0(1)
  • Worst time complexity: when i=1, move n-1 times. 0(n)
  • Average time complexity: (n-1)/2.

3. Summary of knowledge points

insert image description here

Guess you like

Origin blog.csdn.net/weixin_68153081/article/details/126626682