Data Structures – Linear tables A linear table is a queue

It’s similar to a Node.js queue, but it feels like one, and it’s not clear if it is

Straight to the question, (^∇^*)

Using a linear table to store a set of student information and to support regular additions and deletions requires the following subfunctions

  1. Create a sequence table
  2. Find the length of a linear list
  3. Output linear list
  4. Inserts an element at the specified position in a linear table
  5. Finds the specified element by key value
  6. Gets information about the element at the specified location
  7. Deletes the element at the specified location
  8. Release linear tables that need to store student information there
  9. Student id
  10. The name
  11. age
  12. professional
  13. School year

Define basic data types

You need to know the student id, name, age, major, and year of enrollment, so you need to define basic data types

typedef struct {
    char num[20];
    char name[20];
    int age;
    char major;
    int registerYear;
}ElemType;
Copy the code

Define physical storage

Defines the physical storage of linear tables

typedef struct { ElemType data[MAXCOUNT]; // define data int len; // Define length};Copy the code

This completes the physical storage of linear tables

Define the main function to implement the function

Ignore the input and output for the moment

int main() {
    int option;
    printf("Student Management System entry successful \n");
    printf("* * * * * * * * * * * * option * * * * * * * * * * * * * * * * * * * \ n");
    printf("1 Create a linear table \n");
    printf("2 Find the length of a linear table \n");
    printf("3 Output detail table \n");
    printf("4 Insert an element \n at the position of the linear table");
    printf("5 Find the specified element \n based on the key-value pair");
    printf("6 Get information about the element at the specified location \n");
    printf("7 Delete element \n at specified position");
    printf("8 release linear table \n");
    printf("9 Exit system \n");
    printf("**************end! *********************\n");
    
    while(1) {
        printf("Please enter option \n");
        scanf("%d", &option);
        switch(option) {
            case 1:
                createList();
                break;
            case 2:
                listLength();
                break;
            case 3:
                printfList();
                break;
            case 4:
                insterList();
                break;
            case 5:
                selectList();
                break;
            case 6:
                infoList();
                break;
            case 7:
                deleteList();
                break;
            case 8:
                freeList();
                break;
            case 9:
                return 0;
            default:
                printf("There is an error you entered, please re-enter");
                break; }}return 0;
}
Copy the code

Let’s implement the separate functions

Build a linear table

Apply for memory space

/* * create a linear table */ /inInt createList(SeqList *myLIst) {myLIst = (SeqList *)malloc(sizeof(SeqList)); // Request a storage spaceif (myLIst == NULL) {
        return- 1; // Failed to create storage space}else {
        return1; // Storage space created successfully}}Copy the code

Find the length of a linear table

// inInt listLength(const SeqList *myList) {// The list is not createdif (myList == NULL)
        return- 1; // Create a linear tablereturn myList -> len;
};
Copy the code

Output linear list

/* * output linear table */ //inOut Indicates the contents of a linear table intprintfList(const SeqList *myList) { int i, len; // Check whether the pointer is nullif (myList == NULL)
        return- 1; Len = myList -> len;if (len == 0) {
        printf ("The linear table you entered is empty \n"); } // Output the linear tablefor (i = 0; i < len; i++) {
        printf("Output array \n of group %d", i + 1);
        printf("Student Id = %s\n", myList->data[i].num);
        printf("Age = %d\n", myList ->data[i].age);
        printf("Professional = %s\n", myList ->data[i].major);
        printf("Name = %s\n", myList ->data[i].name);
        printf("Year of admission = %d\n", myList ->data[i].registerYear);
    }
    printf("Linear table output successful \n");
    return 1;
}
Copy the code

Inserts at the specified position in the linear table

Design the master function of a subfunction

/* * Inserts */ / at the specified position in the linear tableinInt insterList(SeqList *myList) {int TMP, col; ElemType *tmpList = NULL; // Check whether the incoming address is emptyif (myList == NULL) {
        return- 1; } /* * insert */ / get user input TMP = inputList(&tmplist, &col); // Check whether the command succeededif (tmp == -1)
        return- 1; TMP = moveList(myList, col, 1); // 1 indicates move backward. 0 indicates move forwardif (tmp == -1) {
        return- 1; } // Insert values into myList->data[col-1] = *tmpList; MyList ->len ++; // Free storage space (tmpList); // set tmpList = NULL;return 1;
}
Copy the code

We then proceed to design the subfunctions that insert the information

// Get user input to insert linear table information //inOut Result int inputList(ElemType **tmpList, Int *col) {tmpList = (ElemType *)malloc(sizeof(ElemType));if (*tmpList == NULL){
        printf("Insufficient space to apply \n");
        return- 1; } // Get the user's content to insert fflush(stdin);printf("Please enter student number \n");
    gets(((*tmpList) ->num));
    fflush(stdin);
    printf("Please enter name \n");
    gets((*tmpList) ->name);
    fflush(stdin);
    printf("Please enter age \n");
    scanf("%d",&((*tmpList) ->age));
    fflush(stdin);
    printf("Please enter major \n");
    gets((*tmpList)->major);
    fflush(stdin);
    printf("Please enter year \n");
    scanf("%d", &((*tmpList)->registerYear));
    fflush(stdin);
    printf("Please enter the location to insert \n");
    scanf("%d", col);
    fflush(stdin);
    return1; // Get the input from the user.Copy the code

Then design the subfunctions of the movement element, divided into forward movement and backward movement

Int moveList(SeqList *myList, int col, int option) {int I; // Check whether the incoming linear table is emptyif (myList == NULL) {
        printf ("The linear table is empty \n");
        return- 1; } // Check whether the linear table is fullif (listLength(myList) >= MAXCOUNT) {
        printf("Linear table is full and cannot be inserted, please delete and insert \n");
        return- 1; } // determine whether col is out of bounds, causing the linear table to be incoherentif((col > listLength(myList) && col - listLength(myList) ! = 1 ) || col < 1) {printf("Incorrect insertion position entered, please confirm and reinsert \n");
        return- 1; } switch(option) {case1: // select * from col;for(i = listLength(myList); i >= col; i--) {
               myList ->data[i] = myList ->data[i-1];
           };
           break;
        case0: // The linear table moves forwardfor(i = col; i < listLength(myList); i++) {
                myList ->data[i - 1] = myList ->data[i];
            }
            break;
       default:
           printf("Input type error \n");
           return- 1; }return 1;
}
Copy the code

Find linear tables based on key values

Int selectList(SeqList *myList) {if (myList == NULL) {
        return- 1; } char key[20]; int i; // Get the key valueprintf("Please enter the key value, i.e. student number \n"); gets(key); // perform traversalfor (i = 0; i< listLength(myList); i++) {
        if (strcmp(myList ->data[i].num, key) == 0) {
            break; }}return i;
}
Copy the code

Gets information about the element at the specified location

Int infoList(SeqList *myList) {int key; // Get the required index valueprintf("Please enter the index value \n");
    scanf("%d\n", &key); // Check the input keyif (key > listLength(myList) || key < 0) {
        return- 1; // The input index is incorrect} // The output value corresponds to the student numberprintf("Student Id = %s\n", myList ->data[key].num);
    return 1;
}
Copy the code

Remove elements

Int deleteList(SeqList *myList) {char num[20]; int tmp; // Check if myList is emptyif (myList == NULL) {
        return- 1; } // Get the student id to be deletedprintf("Please enter the student id \n to be deleted"); fflush(stdin); gets(num); TMP = selectList(myList, num);if (tmp == -1) {
        return- 1; } // move forward TMP = moveList(myList, TMP, 1);if (tmp == -1) {
        return- 1; }return 1;
}
Copy the code

Release linear list

Int DestroyList(SeqList *myList) {if (myList == NULL) {
        return- 1; } free(myList);return 1;
}
Copy the code

The total file

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCOUNT 50Typedef struct {char num[20]; Char name[20]; // Define name int age; Char major[20]; // Define profession int registerYear; }ElemType; Typedef struct {ElemType data[MAXCOUNT]; // define data int len; }SeqList; int createList(SeqList **myList); int listLength(const SeqList *myList); intprintfList(const SeqList *myList);
int inputList(ElemType **tmpList, int *col);
int moveList(SeqList *myList, int col, int option);
int selectList(SeqList *myList, char *key);
int infoList(SeqList *myList, int key);
int DestroyList(SeqList *myList);

int main() {// define some variables int option, TMP; SeqList *myList; MyList = NULL; myList = NULL; char key[20]; int num; // Output optionsprintf("Student Management System entry successful \n");
    printf("* * * * * * * * * * * * option * * * * * * * * * * * * * * * * * * * \ n");
    printf("1 Create a linear table \n");
    printf("2 Find the length of a linear table \n");
    printf("3 Output detail table \n");
    printf("4 Insert an element \n at the position of the linear table");
    printf("5 Find the specified element \n based on the key-value pair");
    printf("6 Get information about the element at the specified location \n");
    printf("7 Delete element \n at specified position");
    printf("8 release linear table \n");
    printf("9 Exit system \n");
    printf("**************end! *********************\n"); // Input options // basic logic processingwhile(1) {
        printf("Please enter option \n");
        scanf("%d", &option); Switch (option) {// Create a linear tablecase1: tmp = createList(&myList); // Capture the result and create a linear table // output the resultif (tmp == 1) {
                    printf("Success in creating a linear table! \n");
                } else {
                    printf("Failed to create a linear table! \n");
                };
                break; // Query the length of a linear tablecase 2:
                tmp = listLength(myList);
                if (tmp == -1) {
                    printf("Failed to query linear table length, linear table \n not specified");
                } else {
                    printf("Query linear table with length %d\n", tmp);
                };
                break; // Output the linear tablecase 3:
                tmp = printfList(myList);
                if (tmp == -1)
                    printf("Failed to output linear table, linear table \n not specified");
                else
                    printf("Output linear table as above \n");
                break; // Insert a linear tablecase 4:
                tmp = insterList(myList);
                if (tmp == -1)
                    printf("Failed to insert linear table \n");
                else
                    printf("Insert linear table successful \n");
                break;
            case5: // Find index values based on key valuesprintf("Please enter the key value, i.e. student number \n");
                fflush(stdin);
                gets(key);
                fflush(stdin);
                tmp = selectList(myList, key);
                if (tmp == -1)
                    printf("Find error \n");
                else if (tmp >= listLength(myList))
                    printf("Not found!);
                else
                    printf("Index value is %d\n", tmp);
                break;
            case6: // Find key values based on index valuesprintf("Please enter the index value \n");
                scanf("%d", &num);
                tmp = infoList(myList, num);
                if (tmp == -1) {
                    printf("Search failed \n");
                } else {
                    printf("Output message as above \n");
                }
                break;
            caseTMP = deleteList(myList);if (tmp == -1)
                    printf("Delete failed \n");
                else
                    printf("Deleted successfully");
                break;
            case 8:
                tmp = DestroyList(myList);
                if (tmp == -1) {
                    printf("Release failed \n");
                } else {
                    printf("Release successful \n");
                }
                break;
            case 9:
                return 0;
            default:
                printf("There is an error you entered, please re-enter");
                break; }}return0; } /* * create a linear table */ /inInt createList(SeqList **myList) {*myList = (SeqList *)malloc(sizeof(SeqList)); // Request a storage spaceif (*myList == NULL) {
        return- 1; // Failed to create storage space}else {
        return1; // create storage space successfully}} /* * find the linear table length, */ //inInt listLength(const SeqList *myList) {// The list is not createdif (myList == NULL)
        return- 1; // Create a linear tablereturnmyList -> len; }; /* * output linear table */ //inOut Indicates the contents of a linear table intprintfList(const SeqList *myList) { int i, len; // Check whether the pointer is nullif (myList == NULL)
        return- 1; Len = myList -> len;if (len == 0) {
        printf ("The linear table you entered is empty \n");
        return1; } // Output the linear tablefor (i = 0; i < len; i++) {
        printf("Output array \n of group %d", i + 1);
        printf("Student Id = %s\n", myList->data[i].num);
        printf("Age = %d\n", myList ->data[i].age);
        printf("Professional = %s\n", myList ->data[i].major);
        printf("Name = %s\n", myList ->data[i].name);
        printf("Year of admission = %d\n", myList ->data[i].registerYear);
    }
    printf("Linear table output successful \n");
    return1; } /* * inserts */ / at the specified position in the linear tableinInt insterList(SeqList *myList) {int TMP, col; ElemType *tmpList = NULL; // Check whether the incoming address is emptyif (myList == NULL) {
        return- 1; } /* * insert */ / get user input TMP = inputList(&tmplist, &col); // Check whether the command succeededif (tmp == -1)
        return- 1; TMP = moveList(myList, col, 1); // 1 indicates move backward. 0 indicates move forwardif (tmp == -1) {
        return- 1; } // Insert values into myList->data[col-1] = *tmpList; MyList ->len ++; // Free storage space (tmpList); // set tmpList = NULL;return1; } // get user input to insert linear table information //inOut Result int inputList(ElemType **tmpList, Int *col) {tmpList = (ElemType *)malloc(sizeof(ElemType));if (*tmpList == NULL){
        printf("Insufficient space to apply \n");
        return- 1; } // Get the user's content to insert fflush(stdin);printf("Please enter student number \n");
    gets(((*tmpList) ->num));
    fflush(stdin);
    printf("Please enter name \n");
    gets((*tmpList) ->name);
    fflush(stdin);
    printf("Please enter age \n");
    scanf("%d",&((*tmpList) ->age));
    fflush(stdin);
    printf("Please enter major \n");
    gets((*tmpList)->major);
    fflush(stdin);
    printf("Please enter year \n");
    scanf("%d", &((*tmpList)->registerYear));
    fflush(stdin);
    printf("Please enter the location to insert \n");
    scanf("%d", col);
    fflush(stdin);
    return1; } // Move the linear table function // where option 1 moves back, 0 moves forward col is the position to insert, Int moveList(SeqList *myList, int col, int option) {int I; // Check whether the incoming linear table is emptyif (myList == NULL) {
        printf ("The linear table is empty \n");
        return- 1; } // Check whether the linear table is fullif (listLength(myList) >= MAXCOUNT) {
        printf("Linear table is full and cannot be inserted, please delete and insert \n");
        return- 1; } // determine whether col is out of bounds, causing the linear table to be incoherentif((col > listLength(myList) && col - listLength(myList) ! = 1 ) || col < 1) {printf("Incorrect insertion position entered, please confirm and reinsert \n");
        return- 1; } switch(option) {case1: // select * from col;for(i = listLength(myList); i >= col; i--) {
               myList ->data[i] = myList ->data[i-1];
           };
           break;
        case0: // move forward col--;for(i = col; i < listLength(myList); i++) {
                myList ->data[i] = myList ->data[i+1];
            }
            break;
       default:
           printf("Input type error \n");
           return- 1; }return1; Int selectList(SeqList *myList, char * key) {if (myList == NULL) {
        return- 1; } int i; // perform traversalfor (i = 0; i< listLength(myList); i++) {
        if (strcmp(myList ->data[i].num, key) == 0) {
            break; }}returni; Int infoList(SeqList *myList, int key) {if (myList == NULL) {
        return- 1; } // Check the input keyif (key > listLength(myList) || key < 0) {
        return- 1; // The input index is incorrect} // The output value corresponds to the student numberprintf("Student Id = %s\n", myList ->data[key].num);
    return1; Int deleteList(SeqList *myList) {char num[20]; int tmp; // Check if myList is emptyif (myList == NULL) {
        return- 1; } // Get the student id to be deletedprintf("Please enter the student id \n to be deleted"); fflush(stdin); gets(num); TMP = selectList(myList, num);if (tmp == -1) {
        return- 1; } // move forward TMP = moveList(myList, TMP, 1);if (tmp == -1) {
        return- 1; }return1; Int DestroyList(SeqList *myList) {if (myList == NULL) {
        return- 1; } free(myList);return 1;
}
Copy the code

Run the following

The source file gitlab.com/melovemingm…