Get the App
SLTechnology News&Howtos  ›  Development  › 

How to insert and delete sequential tables in C language

Shulou Source: shulou.com Published: 2022-05-31 17:34:57 09月21日 Update

In this article, the editor introduces in detail "how to insert and delete the order table in C language". The content is detailed, the steps are clear, and the details are handled properly. I hope that this "C language how to achieve the insertion and deletion of the sequence table" article can help you solve your doubts.

First declare the structure of a sequential table (the first element of the array is 0, but the first element of the sequential table usually starts with 1 (artificially set))

# include # include # define MAXSIZE 10#define OK 1#define FALSE 0 typedef int Elemtype;typedef bool Status; typedef struct list {Elemtype * elem; int len; / / number of data int listsize; / / sequence table length} List

Listsize represents the maximum capacity of this sequence table, which can be expanded at any time.

Len represents several valid data in the sequence table you created, always less than or equal to listsize

Initialize the sequence table attribute void list_init (List * L) {L-> elem= (Elemtype *) malloc (MAXSIZE*sizeof (Elemtype)); / / Open up the space if (L-> elem==NULL) / / determine whether the space is opened successfully {printf ("malloc fail\ n"); exit (0);} L-> len=0 / / the valid data of the initialization data is 0L-> listsize=MAXSIZE; / / the length of the initialization array is MAXSIZE} 2, and the insert Status list_insert of the sequential table (List * Lint I meme Elemtype data) {Elemtype * base,*insert,*p; if (iL- > len+1 | | L==NULL) {printf ("position input error\ n"); return FALSE } if (L-> len > L-> listsize) {base= (Elemtype *) realloc (L-> elem, (L-> listsize+MAXSIZE) * sizeof (Elemtype)); / / dynamic expansion L-> elem=base; L-> listsize+=MAXSIZE;// update sequence table size} insert=& (L-> ELEM [I-1]) / / the target pointer points to the destination address to be inserted / / the address of the last element for (pendant L-> elem + L-> len-1;p > = insert;p--) {* (pause 1) = * p;} * insert=data; L-> len++; return OK;}

3. Delete Status delete_list (List * LPowerint I) {ElemType * Q len deletecii; if (L==NULL | | iL- > len) return FALSE; delete_i=& (L-> ELEM [I-1]); / / use the pointer to point to the address of the location to be deleted qdelete L-> elem + L-> len-1. The / / Q pointer points to the address of the last position in the sequence table, the first address plus the length of the array is the last element address for (delete_i=delete_i+1;delete_ilen--; return OK;}

Whole program

# include # include # define MAXSIZE 10#define OK 1#define FALSE 0 typedef int Elemtype;typedef bool Status; typedef struct list {Elemtype * elem; int len; int listsize;} List; void list_init (List * L) {L-> elem= (Elemtype *) malloc (MAXSIZE*sizeof (Elemtype)) / / Open space if (L-> elem==NULL) / / determine whether the space is opened successfully {printf ("malloc fail\ n"); exit (0);} L-> len=0; / / initialization data valid data is 0 L-> listsize=MAXSIZE / / initialize the array length as MAXSIZE} Status list_insert (List * L L==NULL int iMagne Elemtype data) {Elemtype * base,*insert,*p; if (iL- > len+1 | | L==NULL) {printf ("position input error\ n"); return FALSE } if (L-> len > L-> listsize) {base= (Elemtype *) realloc (L-> elem, (L-> listsize+MAXSIZE) * sizeof (Elemtype)); L-> elem=base; L-> listsize+=MAXSIZE;} insert=& (L-> Elm [I-1]) / / the target pointer points to the destination address to be inserted / / the address of the last element for (pendant L-> elem + L-> len-1;p > = insert;p--) {* (pendant 1) = * p;} * insert=data; L-> len++; return OK;} Status list_delete (List * L drawing int I) {Elemtype * aim,*p If (iL- > len) {printf ("position input error\ n"); return FALSE;} aim=& (L-> ELEM [I-1]); / / the target pointer points to the target address to be deleted p = (L-> elem+L- > len-1); / / points to the address of the last element for (aim=aim+1;aimlen--) Return OK;} void show_list (List * L) {int i; for (elem [% d] =% d\ n ") {printf (" int i; List L; list_init [% d] =% d\ n ");} printf ("\ n ");} int main () {int i; List L; list_init (& L); I

Tags: Order address point location data target element pointer array space length language valid article error input success representative content proper Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno NVidia MySQL Huawei Redmi