[Data structure] Ordered insertion, expansion and sorting of sequential tables

Sequential Insertion of Sequential Tables

Status SeqListInsert(SqList &L,ElemType e)//有序插入的函数
{
    int i=0;
    while(i<L.length&&L.elem[i]<e)//判断插入位置合法性
    {
        i++;
    }
    ListInsert(L,i+1,e);
    //调用插入函数
    return OK;
}
Status ListInsert(SqList &L,int i,ElemType e)//插入函数
{
    int j;
    if((i<1)||(i>L.length+1)) return ERROR;
    if(L.length==MAXSIZE) return ERROR;
    for(j=L.length-1;j>=i-1;j--)
    {
        L.elem[j+1]=L.elem[j];
    }
    L.elem[i-1]=e;
    ++L.length;
    return OK;
}

I believe that everyone has a good understanding of the simple insertion function. If you want to achieve sequential insertion, you only need to add another judgment, that is, to judge where to insert, as above.


Expansion of the sequence table

#define INcrease 100
void expandList(SqList& L)//扩容
{ 
    ElemType *newelem=new ElemType[INcreast];
    //INcrease为你想要扩容后表的大小
    int i = 0;
    for(i = 0; i < L.length; i++) 
    {
        newelem[i] = L.elem[i];
    }//将旧表的数据放入新表中
    delete L.elem; 
    L.elem = newelem; 
}

The method I adopt here is to re-apply for a larger space, and put the data of the old table into the new table, the operation is a for loop in the code segment, and delete the old table, in order not to affect the use of L in the main function. elem, I re-expressed newelem as L.elem at the end.


Sorting of Sequence Tables

void Listpaixu(SqList &L)// 排序
{
    int i,j,t;
    for(i=1;i<L.length;i++)
    {
        for(j=1;j<=L.length-i;j++)//每次循环都会从第一个元素开始判断
        {
            if(L.elem[j]<L.elem[j-1])
            {
                t=L.elem[j];
                //用t来保存一下要移位的数据
                L.elem[j]=L.elem[j-1];
                //移位
                L.elem[j-1]=t;
                //把之前保存的数据放到新的位置
                
            }
        }
    }
}

When you are conceiving, you might as well simply write a list of values ​​​​from large to small on paper

1. Change position

In order to implement such an algorithm, it is definitely necessary to judge the magnitude relationship between the values ​​of two similar elements, and then change their positions. When it comes to changing positions, it is necessary to set a variable to store the element value before the shift, so that The position of the element can be changed

2. Nested loops

Through this nested loop, all the elements in unreasonable positions can be fully and smoothly returned, but it is recommended that you first consider what kind of function you want to achieve when writing the algorithm, that is, write out the specific operations to be realized first, and then Consider what kind of loop conditions are needed, and finally put the operation into the loop, otherwise the problem will become very complicated.


In this article, what I most want to share with beginners like me is that when you write code, it is recommended to write execution statements first , that is, what kind of operation do you want to perform, and finally consider how many times you need to loop , which is conducive to clearer thinking, of course, this is just the idea I have summarized in practice.

Guess you like

Origin blog.csdn.net/2301_77112634/article/details/130244192