SoFunction
Updated on 2025-04-06

C language order table implements code error sorting


/********************************************
* File name:
* File description: Linear table sequential storage demonstration
* File author: by ,in 2013.11.16
* File version: 1.0
* Modify the record:
*********************************************/
#ifndef __SQLIST_H__
#define __DWLIST_H__

#include <>
#include <>
#include <>

#define MAXSIZE     50
#define OK          0
#define ERR         -1

typedef int elemtype;

typedef struct {
    elemtype data[MAXSIZE];
    int      len;
}sqlist;

int init_list(sqlist *L);
int destroy_list(sqlist *L);
int list_empty(sqlist L);
int list_length(sqlist L);
int disp_list(sqlist L);
int get_elem(sqlist L, int i, elemtype *e);
int local_elem(sqlist L, elemtype e);
int list_insert(sqlist *L, int i, elemtype e);
int list_delete(sqlist *L, int i, elemtype *e);

#endif


/**************************************************
* File name:
* File description: Implementation of linear table sequential storage
* File author: by ,in 2013.11.16
* File version: 1.0
* Modify the record:
***************************************************/
#include ""

#if 0
#define ERR_NONE_ERROR        0
#define ERR_FUNC_EXEC         1
#define ERR_FILE_OPEN         2

char *error_msg[] = {
/* 0 */    "Successful execution, no errors",
/* 1 */   "Function execution error",
/* 2 */   "File opening error",
};
int my_errno = 0;
#endif

int main(void)
{
    int ret = 0;
    int i = 0;
    sqlist slist;
    elemtype e;

    memset(&slist, 0, sizeof(slist));
    printf("length:%d\n", );
    ret = init_list(&slist);
    if (OK != ret)
        return -1;

    ret = list_empty(slist);
printf("Length:%d\n", );
    if (OK == ret)
printf("Sequence table is empty\n");
    if (ERR == ret)
printf("Sequence table is not empty\n");

    for (i = 0; i < 10; i++) {
        e = (elemtype)i;
        list_insert(&slist, i, e);
    }
printf("insert data\n");

    ret = list_empty(slist);
    if (OK == ret)
printf("Sequence table is empty\n");
    if (ERR == ret)
printf("Sequence table is not empty\n");

    printf("after length%d\n", list_length(slist));

    disp_list(slist);

    destroy_list(&slist);

    return 0;
}

/*=====================================================
* Function name: init_list
* Function function: Initialize a sequential table and create an empty sequential table.
* Function parameters: sqlist *L   Responsible for returning a created order table if created
Return NULL if it fails
* Return Value: successfully returns 0 and returns a created empty table through a pointer
Failed to return -1 pointer returns NULL
* Created by: by,in 2013.11.16
* Modify the record:
======================================================*/
int init_list(sqlist *L)
{
    L = (sqlist *)malloc(sizeof(sqlist));

    if (NULL == L) {
        L = NULL;
        return -1;
    }

    L->len = 0;

    return 0;
}

/*=====================================================
* Function name: destroy_list
* Function function: destroy the created order table and release the space of the order table
* Function parameters: sqlist *L, an existing linear table
* Return Value: Success     0
Failure   -1
Usually free will not fail, in fact, this function can be directly used with void
Yes, this is just written by myself. You will know that it will not return 0 when you see the code.
* Created by: by,in 2013.11.16
* Modify the record:
======================================================*/
int destroy_list(sqlist *L)
{
    free(L);

    return 0;
}

/*=====================================================
* Function name: list_empty
* Function function: determine whether the sqlist order table is empty
* Function parameters: sqlist L, existing linear table
* Return Value: empty     0
Not empty -1
* Created by: by,in 2013.11.16
* Modify the record:
======================================================*/
int list_empty(sqlist L)
{
    if (0 == )
        return 0;

    return -1;
}

/*=====================================================
* Function name: list_length
* Function function: Get the length of the linear table and return the number of elements in the sequential table
* Function parameters: sqlist L, an existing linear table
* Return Value: L's length
* Created by: by,in 2013.11.16
* Modify the record:
======================================================*/
int list_length(sqlist L)
{
    return ;
}

/*=====================================================
* Function name: disp_list
* Function function: display all elements in the order table
* Function parameters: sqlist L, an existing linear table
* Return Value: Success     0
Failure   -1
* Created by: by,in 2013.11.16
* Modify the record:
======================================================*/
int disp_list(sqlist L)
{
    int i = 0;

    if (0 >= )
        return -1;

    for (i = 0; i < ; i++)
        printf("%d\t", [i]);
    /*
* I have objections to this place myself. First of all, you may not know the output type is
* %d, then find out whether the length is to use the list_length function or the usage method,
* list_length is the extra overhead of function calls, on PC
* Overhead is nothing, but in embedded systems, this overhead has to be considered.
* This is basically a problem between good portability and code efficiency, in order to improve
* The portability can add several layers of abstraction to achieve various judgments unless it is extremely large.
* Projects or to match various such devices, I think like code definition type
* A small thing can be solved by team communication. Work is to avoid problems, and learning is to find problems by yourself.
* So the choice can only be determined by the individual.
    */
    printf("\n");

    return 0;
}

/*=====================================================
* Function name: get_elem
* Function function: Get the value range of the element at the i position, so that it can be convenient for corresponding i to start from 0 and
The array subscript is consistent, and the obtained value is returned with e.
* Function parameters: sqlite L     The order of existence
int                                                              �
elemtype *e Return value range
* Return Value: Success     0
Failure   -1
* Created by: by,in 2013.11.16
* Modify the record:
======================================================*/
int get_elem(sqlist L, int i, elemtype *e)
{
    if (i < 0 || i >= ) {
        e = NULL;
        return -1;
    }

    *e = [i];
    /*
* Pay attention to this place
* Look at the difference from e = &([i])
    */

    return 0;
}

/*=====================================================
* Function name: local_elem
* Function function: Search by element value, return the first element position that matches e
* Function parameters: sqlist L, existing order table
* Return Value: Existing Return Location
Failed to return -1
* Created by: by,in 2013.11.16
* Modify the record:
======================================================*/
int local_elem(sqlist L, elemtype e)
{
    int i = 0;

    for (i = 0; i < ; i++) {
        if (e == [i])
            return i;
    }

    return -1;
}

/*=====================================================
* Function name: list_insert
* Function function: Insert element at i position in sqlite
* Function parameters: sqlist *L   Existing order table
int                                                              �
elemtype e  Element
* Return Value: Success   0
Failure -1
* Created by: by,in 2013.11.16
* Modify the record:
======================================================*/
int list_insert(sqlist *L, int i, elemtype e)
{
    int j = 0;

    if (i < 0 || i > MAXSIZE-1)
        return -1;

    for (j = L->len; j > i; j--)
        L->data[j] = L->data[j-1];

    L->data[i] = e;
    L->len++;

    return 0;
}

/*=====================================================
* Function name: list_delete
* Function function: delete the element at position i, and the element is returned through e
* Function parameters: sqlite  *L  Existing order table
int                                                              �
elemtype *e Delete the element at the location
* Return Value: Success    0
Failure  -1
* Created by: by,in 2013.11.16
* Modify the record:
======================================================*/
int list_delete(sqlist *L, int i, elemtype *e)
{
    int j = 0;

    if (i < 0 || i >=L->len)
        return -1;

    *e = L->data[i];
    for (j = i; j < (L->len-1); j++)
        L->data[j] = L->data[j+1];

    L->len--;

    return 0;
}