Interval deletion of 6-15 linear table elements (20 points)

Given a linear table stored sequentially, design a function to delete all elements with values greater than min and less than max.After deletion, the remaining elements in the table are stored sequentially and the relative position cannot be changed.

Function interface definition:

List Delete( List L, ElementType minD, ElementType maxD );

The List structure is defined as follows:

typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /* Save the position of the last element in the linear table */
};

L is a linear table passed in by the user where ElementType elements can be compared by >, ==, <and minD and maxD are the lower and upper bounds of the value domain of the element to be deleted, respectively.The Delete function should delete all elements with values greater than minD and less than maxD in Data[], while ensuring that the remaining elements in the table are stored sequentially and in the same relative position, and finally return to the deleted table.

Examples of referee testing procedures:

#include <stdio.h>

#define MAXSIZE 20
typedef int ElementType;

typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /* Save the position of the last element in the linear table */
};

List ReadInput(); /* The referee is made and the details are not shown.Elements are stored from subscript 0 */
void PrintList( List L ); /* Referee implementation, details not table */
List Delete( List L, ElementType minD, ElementType maxD );

int main()
{
    List L;
    ElementType minD, maxD;
    int i;

    L = ReadInput();
    scanf("%d %d", &minD, &maxD);
    L = Delete( L, minD, maxD );
    PrintList( L );

    return 0;
}

/* Your code will be embedded here */

Input sample:

10
4 -8 2 12 1 5 9 3 3 10
0 4

Output sample:

4 -8 12 5 9 10 

Thought: What you don't want, copy what you need directly, form a new order table, and overwrite the old one; of course, the table length will also change.

Here, prepare a set of code that will run directly

#include <stdio.h>

#define MAXSIZE 20
typedef int ElementType;

typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /* Save the position of the last element in the linear table */
};

List ReadInput(); /* The referee is made and the details are not shown.Elements are stored from subscript 0 */
void PrintList( List L ); /* Referee implementation, details not table */
List Delete( List L, ElementType minD, ElementType maxD );

int main()
{
    List L;
    ElementType minD, maxD;
    int i;

    L = ReadInput();
    scanf("%d %d", &minD, &maxD);
    L = Delete( L, minD, maxD );
    PrintList( L );

    return 0;
}

/* Your code will be embedded here */

List ReadInput(){
     List L;
     int n;
     scanf("%d", &n);
     for(int i = 0; i < n; i++){
         scanf("%d",&L->Data[i]);
       }
     L->Last = n;
	 return L;
 }
void PrintList( List L )
{
	int i;
	for(i = 0; i <L->Last; i++)
	{
		printf("%d ",L->Data[i]);
	}
	printf("\n");
}

List Delete( List L, ElementType minD, ElementType maxD )
{
	int i,p;
	for(i = 0,p = 0; i <= L->Last ;i++)
	{
		if(L->Data[i] <= minD||L->Data[i] >= maxD)
		{
			L->Data[p++] = L->Data[i];
		}
		
	}
	L->Last = p-1;
	return L;
}

One thing i don't understand is why both my ReadInput function and PrintList function are read from i to Last-1 and the Delete function is read from i to Last.

184 original articles were published. 27% were praised. 70,000 visits+
Private letter follow

Keywords: less

Added by xluminex on Fri, 07 Feb 2020 03:46:26 +0200