[PTA] 6-1 Sequence Table Basic Operations (10 points)

Keywords: network

Topic comes from Network and Information Security-Data Structure Job 1-Basic Concept of Data Structure 6-1
https://fancyking.ml/archives/71

Original title description

6-1 Sequence Table Basic Operations (10 points)
This topic requires four basic operation functions: adding, deleting, searching and output of sequence table elements. L is a sequential table. The function Status List Insert_Sq (SqList & L, int pos, ElemTyp e e) inserts an element E (pos should start from 1) at the POS position of the sequential table. The function Status List Delete_Sq (SqList & L, int pos, ElemType & e) is an element that deletes the POS position of the sequential table and brings it back with the reference parameter E (pos should start from 1), and the function int Locate_Sq (SqList, ElemTyp L), ElemType E) is the bit order of the query element E in the sequential table and returns (if there are more than one first position, the bit order is returned, starting from 1, returning 0 if there is no one), and the function void ListPrint_Sq(SqList L) is the output sequential table element. The problem of full table expansion should be considered in the implementation.

Function interface definition:
Status ListInsert_Sq(SqList &L, int pos, ElemType e);
Status ListDelete_Sq(SqList &L, int pos, ElemType &e);
int ListLocate_Sq(SqList L, ElemType e);
void ListPrint_Sq(SqList L);
Where L is a sequence table. pos is the location; e represents the element. When pos parameters in insertion and deletion operations are illegal, the function returns ERROR or OK.

Examples of referee testing procedures:

//The library function header file contains
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>


//Definition of Function State Code
#define TRUE        1
#define FALSE       0
#define OK          1
#define ERROR       0
#define INFEASIBLE -1
#define OVERFLOW   -2

typedef int  Status;

//Definition of storage structure for sequential tables
#define LIST_INIT_SIZE  100
#define LISTINCREMENT   10
typedef int ElemType;  //Assuming that all elements in a linear table are integers
typedef struct{
    ElemType* elem;   //Storage space base address
    int length;       //Number of elements in a table
    int listsize;     //Table capacity size
}SqList;    //Sequence table type definition
Status ListInsert_Sq(SqList &L, int pos, ElemType e);
Status ListDelete_Sq(SqList &L, int pos, ElemType &e);
int ListLocate_Sq(SqList L, ElemType e);
void ListPrint_Sq(SqList L);

//Structure Initialization and Destruction Operation
Status InitList_Sq(SqList &L){
  //Initialize L to an empty ordered list
    L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L.elem)exit(OVERFLOW);
    L.listsize=LIST_INIT_SIZE;
    L.length=0;
    return OK;
}


int main() {
    SqList L;

    if(InitList_Sq(L)!= OK) {
        printf("InitList_Sq: Initialization failed!!!\n");
        return -1;
    }

    for(int i = 1; i <= 10; ++ i)
        ListInsert_Sq(L, i, i);

    int operationNumber;  //Operation times
    scanf("%d", &operationNumber);

    while(operationNumber != 0) {
        int operationType;  //Type of operation
        scanf("%d", & operationType);

        if(operationType == 1) {  //Increase operation
            int pos, elem;
            scanf("%d%d", &pos, &elem);
            ListInsert_Sq(L, pos, elem);
        } else if(operationType == 2) {  //Delete operation
             int pos; ElemType elem;
             scanf("%d", &pos);
             ListDelete_Sq(L, pos, elem);
             printf("%d\n", elem);
        } else if(operationType == 3) {  //Find and locate operation
            ElemType elem;
            scanf("%d", &elem);
            int pos = ListLocate_Sq(L, elem);
            if(pos >= 1 && pos <= L.length)
                printf("%d\n", pos);
            else
                printf("NOT FIND!\n");
        } else if(operationType == 4) {  //Output operation
            ListPrint_Sq(L);
        }
       operationNumber--;
    }
    return 0;
}

/* Please fill in the answers here. */

Input format: The first line enters an integer operationNumber, representing the operand, and the next operationNumber line, each line representing an operation information (including "operation type number operation content"). Number 1 denotes insertion operation, the latter two parameters denote insertion position and insertion element value number 2 denotes deletion operation, the latter one denotes deletion position number 3 denotes search operation, and the latter one denotes search value number 4 denotes output format of sequential table output operation: for operation 2, output value of deleted element for operation 3, output The position of the element, if it does not exist, outputs "NOT FOUND"; for operation 4, sequentially outputs the elements of the entire sequence table, separated by spaces between the two elements, and there is no space behind the last element.

Input sample:

4
1 1 11
2 2
3 3
4

Output sample:

1
3
11 2 3 4 5 6 7 8 9 10

Note section

1. realoc function

  • The use of realloc functions requires the introduction of the header file stdlib.h

  • The prototype of this function is realloc (void*_ptr, size_t__size)

void* __cdecl realloc(
    _Pre_maybenull_ _Post_invalid_ void*  _Block,
    _In_ _CRT_GUARDOVERFLOW        size_t _Size
    );
  • That is, the first parameter passed in is the pointer type, and the second parameter is the changed size.
    • If the operation reduces the memory allocation, then the function simply changes the index information.
    • If the operation increases the allocation of memory, then when there is enough memory to expand after the current memory, the function expands the memory directly and returns the pointer; when there is not enough free memory after the current memory segment, the function looks for the first memory segment in the heap that meets the size of the applied memory, replicates the data, releases the old memory and returns the pointer. Failed application returned to NULL
    • The returned pointer is a pointer, but in any case, it is not guaranteed to return to the original pointer (you can try through the output address). The original pointer will be automatically released by the function, and the original pointer can not be released twice.

AC code

void ListPrint_Sq(SqList L){
    ElemType maxn = L.length;
    for (ElemType i = 0; i < maxn; ++i){
        if(i){
            printf(" ");
        }
        printf("%d", L.elem[i]);
    }
    puts("");
    return;
}

int ListLocate_Sq(SqList L, ElemType e){
    ElemType maxn = L.length;
    for (ElemType i = 0; i < maxn; ++i){
        if(L.elem[i] == e){
            return i + 1;
        }
    }
    return FALSE;
}

Status ListDelete_Sq(SqList &L, int pos, ElemType &e){
    ElemType maxn = L.length;
    if(pos < 1 || pos > maxn){
        return ERROR;
    }
    e = L.elem[pos - 1];
    for (ElemType i = pos - 1; i < maxn - 1; ++i){
        L.elem[i] = L.elem[i + 1];
    }
    L.length -= 1;
    // printf("DEL  %d\n", L.length);
    // ListPrint_Sq(L);
    return OK;
}

Status ListInsert_Sq(SqList &L, int pos, ElemType e){
    ElemType maxn = L.length;
    if(pos < 1 || pos > maxn + 1){
        return ERROR;
    }
    if(L.length >= L.listsize){
        ElemType *newe;
        newe = (ElemType *)realloc(L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType));
        if(!newe){
            return OVERFLOW;
        }
        else {
            L.elem = newe;
            L.listsize += LISTINCREMENT;
        }
    }
    for (ElemType i = maxn; i >= pos; --i){
        L.elem[i] = L.elem[i - 1];
    }
    L.elem[pos - 1] = e;
    L.length += 1;
    // ListPrint_Sq(L);
    return OK;
}

/*
11 1 2 3 4 5 6 7 8 9 10
11 2 3 4 5 6 7 8 9 10

*/

Posted by ale_jrb on Thu, 16 May 2019 20:35:54 -0700