The realization and basic operation of sequential linear table

Posted by rondog on Sat, 09 Nov 2019 15:10:22 +0100

Ever since I saw the platform of "think or not" in Zhihu, I always wanted to write something; I also wanted to communicate with more people and learn something.
The teaching material of school data structure is teacher Yan Weimin's "data structure (C language version)". In the teaching material, the class C language between pseudo code and C language is used as the description tool to explain the algorithm. In the actual learning, we still need to convert the code into C language program, which is compiled through. This process can help to better understand the algorithm.
The purpose of my blog is to record my repeated code and finished exercises after class, share with you, and occasionally share my understanding of some open source projects.
The first time the content is very simple, the basic operation of the sequence table. It mainly includes:

  1. Building empty tables
  2. Insertion element
  3. Delete elements
  4. Search element

It has simple functions and less difficulties in compiling.

#include<stdio.h>
#include<stdlib.h>
#include "malloc.h" 

#define TRUE         1
#define FALSE        0
#define OK           1
#define ERROR        0
#define INFEASIBLE   -1
#define OVERFLOW     -2
#define LIST_INIT_SIZE   100
#define LISTINCREMENT    10

typedef int Status;
typedef struct{
    int *elem;
    int length;
    int listsize;
}SqList;

Status InitList_Sq(SqList &L){
    //Construct empty table
    L.elem = (int *)malloc(LIST_INIT_SIZE * sizeof(int));
    if(!L.elem)exit(OVERFLOW);
    L.length = 0;
    L.listsize = LIST_INIT_SIZE;
    return OK; 
}
Status ListInsert_Sq(SqList &L,int i){
    //Insert the number of keyboard entries at position i of the sequence table 
    int* p,* q,e;
    if(i < 1 || i > L.length + 1)return ERROR;
    if(L.length >= L.listsize){
        int* newbase = (int*)realloc(L.elem,(L.listsize + LISTINCREMENT) * sizeof(int));
        if(!newbase)exit(OVERFLOW);
        L.elem = newbase;
        L.listsize += LISTINCREMENT;
    }
    q = &(L.elem[i-1]);
    for(p = &(L.elem[L.length-1]);p >= q;--p) *(p+1) = *p;
    scanf("%d",&e); //Assign keyboard input value to e
    *q = e;
    ++ L.length;
    return OK; 
}
Status List_Print(SqList L){
    //Number in print order table
    for(int r = 0;r < L.length;r++)printf("%d ",L.elem[r]);
    printf("\n");
}
Status List_Search(SqList L){
    //Lookup order table
    int i; 
    scanf("%d",&i); 
    if(i < 1 || i > L.length + 1)return ERROR;
    printf("The number you are looking for is:%d \n",L.elem[i-1]);
}
Status ListDelete_Sq(SqList &L){
    //Delete the specified number and return with e 
    int* p,* q;
    int i,e; 
    scanf("%d",&i);
    if(i < 1 || i > L.length + 1)return ERROR;
    p = &(L.elem[i-1]);
    e = *p;
    q = L.elem + L.length - 1;
    for(++p;p <= q;++p)*(p-1) = *p;
    --L.length;
    printf("%d Deleted\n",e);
    return OK;
}
int main()
{
    SqList L;
    InitList_Sq(L);
    printf("Please enter 4 integers for the order table:\n");
    for(int a = 1;a <= 4;a++)ListInsert_Sq(L,a);
    printf("The existing order table is:\n");
    List_Print(L);
    printf("Please enter the location to find\n");
    List_Search(L);
    printf("Please enter the location and number of inserts(Separate with new line): \n");
    int a;
    scanf("%d",&a);
    ListInsert_Sq(L,a);
    printf("The existing order table is:\n");
    List_Print(L);
    printf("Please enter the location to delete\n");
    ListDelete_Sq(L);
    printf("The existing order table is:\n");
    List_Print(L);
    return 0;} 

The compiler used is dev c + +

Topics: C less