Learning data structure-Chapter 2: Linear table (sequential storage, insertion, deletion)

Chapter 2: Linear Table

1. Definition and basic operation of linear table

Insert picture description here
The linear table has 相同数据类型n (n≥0) data elements有限序列

Insert picture description here
The first element in the linear table is called the head element; the last element is called the tail element.

Every element except the first element 有且仅有一个直接前驱. Every element except the last element 有且仅有一个直接后继.

2. The sequential storage structure of the linear table

The sequential storage of the linear table uses a set 地址连续of storage units (such as the array in the C language) to 依次store the data elements in the linear table. The linear table of sequential storage is also called the sequential table.

Address continuous: each storage unit of the memory has a number, this number is the address

Insert picture description here
Loc(a2)=Loc(a1)+dd is the number of storage units occupied by the element

Loc(ai)=Loca(a1)+(i-1)*d

Through this formula, the position of any element in the sequence table can be calculated, and the time required is the same, that is, the time complexity is O(1), that is随机存取

2.1 How to build the sequential storage structure of a linear table

Static table creation

First we need to "find a piece of land" in memory, and is continuous, then this must have storage space 首地址and 大小; finally we want to list the various elements of condemnation, it would have to know how many elements, that is 表的长度.

The three parts of the sequence table

  • 1. The starting position of the storage space
  • 2. Maximum storage capacity of sequence table
  • 3. The current length of the sequence table
#define MaxSize 50  //定义线性表的最大长度
typedef int Elemtype   //假定表中元素类型是int 
typedef struct{
    
     
       ElemType data [MaxSize];   //顺序表的元素(数组) 
       int length;         //顺序表的当前长度
}SqList;                   //顺序表的类型定义

The array is statically allocated ( 大小固定)

Dynamic table creation

In fact, the storage space (array) is okay 动态分配, that is, the space for storing the array is 程序执行过程中allocated through dynamic allocation statements.

typedef int Elemtype;
typedef struct{
    
     
     ElemType  *data;  //指示动态分配数组的指针
     int MaxSize,length; //数组的最大容量和当前个数
}SeqList;
//动态分配语句(c语言) 
#define initSize 100 
SeqList L;
L.data(ElemType*)malloc(sizeof(ElemType)*initSize);

Note: Dynamic allocation is not 链式存储, it still belongs to the 顺序存储structure, but the size of the allocated space can be in 运行时决定.

3. Summary

Insert picture description here

4. The operation of the sequence table

4.1 Insert

Insert picture description here
The position of a5 is between a1 and a2.
Insert picture description here
Insert picture description here
Algorithm flow: Insert a new element e at the position i ( 1<= i<= L.length+1) of the sequence table L. If the input of i is invalid, return false, indicating that the insertion failed; otherwise, move the i-th element of the sequence table and all subsequent elements to the right by one position, make an empty position to insert a new element e, and increase the length of the sequence table by 1. , Insert successfully, return true.

bool is a boolean type, it has only two values ​​of true and false, which represent true and false respectively

Algorithm idea:

  • 1. Whether the judged value is correct
  • 2. Determine whether the length of the table exceeds the length of the array
  • 3. From back to front to the first position, move these elements back one place respectively
  • 4. Insert the element at position i and modify the table length
bool ListInsert(SqList &L,int i,ElemType e){
    
    
    if(i<1||i>L.length+1){
    
     //判断i的范围是否有效
        return false;
    }
    if(L.length>=MaxSize){
    
      //当前存储空间已经满,不能插入
    	return false;
    }
    for(int j=L.length;j>=i;j--){
    
     //将第i个元素之后的元素后移
    //初值为下标为L.length是因为要将最后一位向后移动,L.length代表移0动后的下标L.length-1代表当前数组中最后一个元素。
    //j>=i,是因为i代表位置i-1才代表最终要移动到的位置的下标,将第i的元素的值变为第i-1个元素的值,就是后移。
         L.data[j]=L.data[j-1]
    }
    L.data[i-1] = e; //在位置 i 插入e
    L.length++;  //数组长度加1
    return true;
}

Performance of sequential store insert operations

The worst case: insert at the end of the table (i.e. i=n+1), the element shift statement will not be executed, and the time complexity is O(1).
Most case: Insert at the head of the table (i.e., i=1), the element shift statement will be executed n times, and the time complexity is O(n).
平均Case: Suppose pi(pi=1/(n+1) [1/(n+1)是因为总共有n+1一个位置可以被插入]) is at the i-th The probability of inserting a node at each position, the average number of times a node needs to be moved when inserting a node into a linear table of length n is:

Insert picture description here
The average time complexity of the sequential table insertion algorithm is O(n)

4.1 Delete

Insert picture description here
Insert picture description here
Insert picture description here
Algorithm flow: Delete L.lengththe element at position i (1≤ i ≤ ) in the sequence table L, return true if it succeeds, and return the deleted element with the reference variable e, otherwise return false.

Algorithm ideas

  • 1. Whether the judged value is correct
  • 2. Take the deleted element
  • 3. Move all the elements behind the deleted element forward one place in turn
  • 4. Modify the table length
bool ListDelete(SqList &L,int i,Elemtype &e){
    
    
     if(i<1||i>L.length){
    
     //判断 i 是否有效
         return false;
     }
     e=L.data[i-1]; //将被删除的元素复制给e
     for(int j=i;j<L.length;j++){
    
    
        //将第i个位置之后的元素前移
        //注意:i 代表位置,不是下标
        L.data[j-1] = L.data[j];
     }
     L.length
     return true;
}

Performance of sequential storage delete operations

The worst case: deleting the end of the table element (i.e. i=n), no need to move the element, the time complexity is O(1)
Most case: deleting the head element (i.e. i=1) requires moving all elements except the first element , The time complexity is O(n)
平均: assuming that pi(pi=1/n [1/n是因为总共有n个位置可以被删除]) is the probability of deleting the node at the i-th position, the node needs to be moved when deleting a node in a linear table of length n The average number of points is

Insert picture description here

5. Summary

顺序存储优点

  • High storage density: No need to add extra storage space for the logical relationship between the elements in the table.
  • Random access: you can quickly access elements at any position in the table

顺序存储缺点

  • Insert and delete operations need to move a large number of elements
  • High storage space requirements will produce "fragments" of storage space

Guess you like

Origin blog.csdn.net/qq_41941875/article/details/106147434