Data structure sequence table topic (4)

Sequence Table Title Description and Code Implementation

Title description: Delete all elements whose value is between a given value s and t (s<t) from the ordered sequence table. If s or t is unreasonable or the sequence table is empty, an error message will be displayed and the operation will exit.
Algorithm idea: This question is similar to the sequence table topic (3), but the difference is that this question is an ordered sequence table, so the deleted elements must be connected as a whole. First look for the first element with a value greater than or equal to s (the first deleted element), and then look for the first element with a value greater than t (the next element of the last deleted element). To delete this element, just The following elements need to be moved forward directly.

Implementation code:

bool del_s_t(sqList &L, ElemType s,Elemtype t)
{
    
    
    int i,j;
    if(s>=t||L.length==0)
        return false;
	for (i = 0; i < L.length&&L.data[i]<s; i++);
	if (i>L.length)
	    return false;
	for (j = i; j < L.length&&L.data[j]<=t; j++);
	for(;j<L.length;i++,j++)
	{
    
    
	     L.data[i]=L.data[j];
	}
	L.length = i;
	return true;
}

Guess you like

Origin blog.csdn.net/ATTAIN__/article/details/107935350