Data structure - insertion, deletion, and search algorithms for sequential tables

//Reference of library functions
#include <stdio.h>
#include <stdlib.h>
//Definition of symbolic constants
#define MAXSIZE 100 //Estimated sequence table capacity
#define OK 1
#define ERROR 0
#define OVERFLOW -2

//Renaming and definition of data types
typedef int Status;
typedef int ElemType;
typedef struct{     ElemType *elem;     int length; }SqList;


//Declaration of custom function
Status InitList(SqList &L);
Status ListInsert(SqList &L,int i ,ElemType e);
Status ListDelete(SqList &L,int i);
Status GetElem(SqList L, int i, ElemType &e);

//Main function
int main(int argc, char *argv[]) {     SqList L;     int choice,z = 1;     int i;     ElemType e;     if(!InitList( L ))     {         exit(0);     }     // Output menu     while(z)     {     printf("-------linear table data storage structure---\n");     printf("|\t\t1. Insert\t\t|\n");     printf("|\t\t2. Delete\t\t|\n");     printf("|\t\t3. Find\t\t|\n");     printf("|\t\t0.Exit \t\t|\n");     printf("Please enter the program you want to choose\n");     scanf("%d",&choice);     switch(choice){         case 1://insert         //for already Know L (known), i, e         printf("Please enter the insertion position: ");         scanf("%d",&i);























        printf("Please input insert element:");
        scanf("%d",&e);
        // call
        if(OK == ListInsert(L , i , e))
        {             printf("Insert successfully!\n\n" );         }         else         {             printf("Insert failed!\n\n");         }         break;         case 2://Delete         printf("Enter the location you want to delete\n");                 scanf("%d",&i) ;                 if(OK == ListDelete(L,i))                 {                     printf("Deleted successfully\n\n");                 }                 else                 {                     printf("Delete failed\n\n");                 }


















                
                break;
        case 3://search
         printf("Enter the serial number of the element you want to find\n");
            scanf("%d",&i);
            //call
                if(OK == GetElem(L , i , e))
                {                     printf("The element you are looking for is: \n");                 }                 else                 {                     printf("The serial number you entered is wrong\n");                 }         break;






        

        case 0:z = 0;
        printf("Welcome to come again next time!");
        break;
        default:printf("Input error!\n");
    }
}
    return 0;
}
//——————Custom function (From "Data Structure")
//Initialization
Status InitList(SqList &L){ //Construct an empty sequence table
    L.elem = new ElemType[MAXSIZE]; //Allocate space for the sequence table
    if(!L.elem) exit (OVERFLOW); //Storage allocation failed
    L.length = 0; //Empty table length is 0
    return OK;
}
//Insertion algorithm
Status ListInsert(SqList &L,int i ,ElemType e){     int j;

    if(i<1 || i>L.length+1) return ERROR; //i value is illegal
    if(L.length==MAXSIZE) return ERROR; //current storage space is full
    for(j=L.length -1;j >= i-1;j--) L.elem[j+1]=L.elem[j]; //Insert position and subsequent elements move backward
    L.elem[i-1]=e ; //Put the new element e into the i-th position
    L.length ++; //Increase the table length by 1
    return OK;
}
//Delete algorithm
Status ListDelete(SqList &L,int i)
{     int j;     if((i <1)||(i>L.length))         return ERROR; //i value is invalid     for (j=i ; j<=L.length-1 ; j++)                         L.elem[j-1]=L. elem[j];//The element after the deleted element is moved forward         --L.length; //The table length is reduced by 1






    return OK ;
}
// Return
the status GetElem ( SqList L , int i , ElemType &e ) {     if ( i < 1 || i > L .length )     return ERROR ;       e = L.elements[i-1];     return OK ; } }




Guess you like

Origin blog.csdn.net/m0_61105833/article/details/120816589