# Introduction to data structure (PTA question bank)

catalogue

6-1 sequence table operation set (20 points)

6-2 interval deletion of linear table elements (20 points)

## 6-1 sequence table operation set (20 points)

This problem requires the implementation of the operation set of the sequence table.

### Function interface definition:

```List MakeEmpty();
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );```

The List structure is defined as follows:

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

Each operation function is defined as:

List MakeEmpty(): create and return an empty linear table;

Position find (List L, ElementType x): returns the position of X in the linear table. If not found, ERROR is returned;

Bool insert (List L, ElementType X, position P): insert X at position P and return true. If the space is FULL, print "FULL" and return false; If parameter p points to an ILLEGAL POSITION, print "ILLEGAL POSITION" and return false;

Bool delete (List L, position P): delete the element at position P and return true. If the parameter p points to an illegal position, print "POSITION P EMPTY" (where p is the parameter value) and return false.

### Example of referee test procedure:

```#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 5
#define ERROR -1
typedef enum {false, true} bool;
typedef int ElementType;
typedef int Position;
typedef struct LNode *List;
struct LNode {
ElementType Data[MAXSIZE];
Position Last; /* Saves the position of the last element in the linear table */
};

List MakeEmpty();
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );

int main()
{
List L;
ElementType X;
Position P;
int N;

L = MakeEmpty();
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
if ( Insert(L, X, 0)==false )
printf(" Insertion Error: %d is not in.\n", X);
}
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
P = Find(L, X);
if ( P == ERROR )
printf("Finding Error: %d is not in.\n", X);
else
printf("%d is at position %d.\n", X, P);
}
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &P);
if ( Delete(L, P)==false )
printf(" Deletion Error.\n");
if ( Insert(L, 0, P)==false )
printf(" Insertion Error: 0 is not in.\n");
}
return 0;
}

/* Your code will be embedded here */```

### Input example:

```6
1 2 3 4 5 6
3
6 5 1
2
-1 6```

No blank lines at the end

### Output example:

```FULL Insertion Error: 6 is not in.
Finding Error: 6 is not in.
5 is at position 0.
1 is at position 4.
POSITION -1 EMPTY Deletion Error.
FULL Insertion Error: 0 is not in.
POSITION 6 EMPTY Deletion Error.
FULL Insertion Error: 0 is not in.```

No blank lines at the end

```List MakeEmpty()
{
List L;
L = (List)malloc(sizeof(struct LNode));
L->Last = -1;
return L;
}
Position Find(List L, ElementType X)
{
Position i;
for (i = 0; i <= L->Last; i++)
if (L->Data[i] == X)
return i;
return ERROR;
}
bool Insert(List L, ElementType X, Position P)
{
if (L->Last == MAXSIZE - 1)
{
printf("FULL");
return false;
}
else if (P < 0 || P > L->Last + 1)
{
printf("ILLEGAL POSITION");
return false;
}
else
{
Position i;
for (i = L->Last; i >= P; i--)
L->Data[i + 1] = L->Data[i];
L->Last++;
L->Data[P] = X;
return true;
}
}
bool Delete(List L, Position P)
{
if (P < 0 || P > L->Last)
{
printf("POSITION %d EMPTY", P);
return false;
}
else
{
Position i;
for (i = P + 1; i <= L->Last; i++)
L->Data[i - 1] = L->Data[i];
L->Last--;
return true;
}
}```

## analysis:

bool type in C language

```typedef enum
{
false,
true
} bool;```

malloc

1. Cast is required when calling malloc function. The type to be converted is the type of the variable on the left of "=".

2. The requested space size should be the type name of the variable. Instead of a pointer type to that type.

3. In the called function, when the function ends, the memory space of the variable will be cleared, and the dynamically applied memory space will be cleared automatically at the end of the program.

## 6-2 interval deletion of linear table elements (20 points)

Given a sequentially stored linear table, 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 in order, 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; /* Saves 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 through >, = =, <; minD and maxD are the lower and upper bounds of the value range of the element to be deleted. The Delete function should Delete all elements in Data [] whose value is greater than minD and less than maxD. At the same time, it should ensure that the remaining elements in the table are stored in order and their relative positions remain unchanged. Finally, it returns the deleted table.

### Example of referee test procedure:

```#include <stdio.h>

#define MAXSIZE 20
typedef int ElementType;

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

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

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

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

return 0;
}

/* Your code will be embedded here */```

### Input example:

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

No blank lines at the end

### Output example:

`4 -8 12 5 9 10 `

No blank lines at the end

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

### analysis:

algorithm

Traverse each element in the sequence table.

Check each element to determine whether it is within the (minD, maxD) interval. If it is, read the next element; If not, put the element in the new sequence table by tail interpolation.

The idea is similar to that of "inverting a table in place".

Tags: C data structure

Posted on Wed, 06 Oct 2021 14:56:22 -0400 by whitemoss