# Implementation of C/C + + linear table

Keywords: C C++ data structure

Definition of linear table:

linear list is the most basic, simplest and most commonly used data structure. Linear table data elements The relationship between is a one-to-one relationship, that is, except for the first and last data elements, other data elements are connected head to tail, but this only applies to most linear tables, not all. At the logical level of data structure, linear tables can be divided into general linear tables and restricted linear tables.

The functions of the linear meter are as follows:

```======1.input data======
======2.output data======
======3.insert data======
======4.Finds the element at the specified location======
======5.Deletes the element at the specified location======
======7.Linear table merging=======
======8.Empty linear table=======
======0.Exit program======```

Use typedef struct to define a linear table sqlist. The code is as follows: (see code explanation for details)

```#define SIZE 100
typedef int ElemType;
typedef int status;

typedef struct sqlist {
ElemType* elem;//Address for storing linear table
string name;//Give a name to the linear table, which is convenient for subsequent output
int length;	   //Length of storage linear table
int listsize;  //Number of linear tables stored
}SqList;//Define linear table```

The SqList is used in the following operations.

After defining the linear table, we need to initialize the linear table:

The function malloc is used in this process. Its usage is different from that in C + +. Using it requires the #include < malloc. H > of the header file

(see code explanation for details)

```status IniList(SqList* L,string name){
L->elem = (int*)malloc(SIZE*sizeof(ElemType));//Strive for dynamic storage space for L
L->length = 0;//Initialization is to set the length of the linear table to 0
L->listsize = SIZE;//The maximum length of the table is 100. To modify it, see the top
L->name = name;//Give the linear table a name
return 1;//Normal exit
}//Initialization of linear table```

Initialize three linear tables in main(): inilist (& table name, string name)

```IniList(&L1,"1");
IniList(&L2,"2");
IniList(&L_Merge,"_Merge");//Initialize the three tables and assign values to table L1```

Assignment of linear table: (see code explanation for details) input(SqList* L,int Num), where num is the number of input numbers

```void input(SqList* L,int Num){
L->length = Num;//Take the number of input data as the length of the linear table
for (int i = 0; i < L->length; i++)
{
scanf_s("%d", &L->elem[i]);//Assign values to linear tables
}
printf("Input completed!\n");
}//Assign values to linear tables```

Output of linear table: (see code explanation for details) Display(SqList* L)

```void Display(SqList* L){
if (L->length == 0) {
return;
}//If the linear table is empty, exit directly
cout << "L" << L->name << ": ";
for (int i = 0; i < L->length; i++) {
printf("%d", L->elem[i]);
printf(" ");
}
printf("\n");
}//Print linear table```

Operation of inserting value in linear table: use ListInsert(SqList*L), (see code explanation for details)

```status ListInsert(SqList* L) {
int pos;//Define the abbreviation pos of position, which is the position where the data is inserted
ElemType* p, * p1, * p2,value;
if (L->length == 0) {
printf("\n This table is empty, unable to insert data, the data insertion program has exited!!!\n");
return 0;
}//Judge whether the table inserted data is empty. If it is empty, exit
printf("\n Please enter the location to insert:");
scanf_s("%d", &pos);
if (pos<1 || pos>L->length+1) {
printf("Error inserting location information!\n");
return 0;
}//Judge whether the position of the inserted value is reasonable. If it is unreasonable, it will be pushed out
printf("Please enter the element to insert:");
scanf_s("%d", &value);
if (L->length >= L->listsize) {
p =(int*)realloc(L->elem, sizeof(ElemType) * (L->listsize + 10));
if (!p) return 0;
L->elem = p;//New address assigned to L
L->listsize += 10;//The size should be adjusted accordingly
} //When the space is insufficient, expand the capacity. This if conditional statement is generally unavailable and can be omitted
p1 = &L->elem[pos - 1];
for (p2 = &L->elem[L->length - 1]; p2 >= p1; p2--) {
*(p2 + 1) = *(p2);//Move back one unit
}
L->elem[pos - 1] = value;
L->length++;
return 1;
}//Insert element```

Delete the value at the specified position in the linear table: (see code explanation for details) DeleteDate(SqList* L)

```status DeleteDate(SqList* L) {
int pos_delete;//Define where to delete data
while (true) {
printf("Please enter the location of the data you want to delete (the length of the table is):%d): ", L->length);
cin >> pos_delete;
if (pos_delete-1<0 || pos_delete-1>=L->length) {
printf("\n The entered position is incorrect! Please re-enter!!\n");
continue;
}
else break;
}//Judge whether the location of the deleted data is reasonable. If it is reasonable, exit the while loop structure
int date_delete = L->elem[pos_delete-1];//Since the subscript starts from 0, we need pos_delete-1 to operate
for (int i = pos_delete-1; i <= L->length-1; i++) {
L->elem[i] = L->elem[i + 1];
}//Since the subscript starts from 0, we need pos_delete-1 to operate
L->length = L->length - 1;
printf("Deleted data:%d\n", date_delete);
return 1;
}//Delete data```

The operations to find data in the linear table are as follows: (see code analysis for details) SearchDate(SqList*L)

```status SearchDate(SqList*L){
int pos_search;//Define where to find
while (true) {
printf("Please enter the location you want to find (the length of the table is):%d): ", L->length);
cin >> pos_search;
if (pos_search-1 < 0 || pos_search-1 >= L->length) {
printf("\n The entered position is incorrect! Please re-enter!!\n");
continue;
}
else break;
}//Judge whether the location of finding data is reasonable. If it is reasonable, exit the while loop structure
printf("This value is:%d\n", L->elem[pos_search-1]);//Since the subscript starts from 0, we need pos_search-1 to output
return 1;
}//Find data```

Operation of adding linear table to data in linear table: (see code analysis for details)   AddList(SqList *L,int Num)      Where num is the number of input numbers

```status AddList(SqList *L,int Num) {
L->length = Num;//Add a linear table whose length = the number of input values
for (int i = 0; i < L->length; i++){
scanf_s("%d", &L->elem[i]);
}
printf("Input completed!\n");
return 1;

Consolidation of linear tables: ListMerge(SqList * Table 1, SqList * Table 2, SqList * consolidated table name) (see code for details)

```status ListMerge(SqList* L1, SqList* L2, SqList* L_Merge) {
L_Merge->length = L1->length + L2->length;//Add the length of the linear table to be merged, and then assign it to the length of the merged table
int pos1 = L1->length, pos2 = L2->length;//pos1 and pos2 are defined to facilitate assignment to the consolidated table
for (int i = 0; i < L1->length; i++) {
L_Merge->elem[i] = L1->elem[i];
}
for (int i = L1->length, k = 0; k < L2->length; k++, i++) {
L_Merge->elem[i] = L2->elem[k];
}/*The condition body of this for loop is critical. The statement int i = L1 - > length uses the length of table L1
Since the angle sign starts from 0, the length of L1 is directly used as the angle sign to start the value*/
IniList(L1,"1");
IniList(L2,"2");//After merging tables, initialize the two tables (equivalent to destroying linear tables)
printf("Merge complete!\n");
return 1;
}//Linear table merging```

Method of destroying linear table: DeleteList(SqList * Table 1, SqList * Table 2, SqList * Table 3)

```status DeleteList(SqList* L1, SqList* L2, SqList* L_Merge) {
IniList(L1,"1");
IniList(L2,"2");
IniList(L_Merge,"_Merge");
printf("Destroy linear table completed!\n");
return 1;
}//The essence of destroying a linear table is to reinitialize the linear table. At this time, the length of each table is 0, so it has been destroyed```

main() function body code:

```int main(){
SqList L1, L2, L_Merge;//Define three tables
int i=1;
while (true) {
start:
printf("===================================\n");
printf("======1.input data======\n");//input
printf("======2.output data======\n");//Display
printf("======3.insert data======\n");//ListInsert
printf("======4.Finds the element at the specified location======\n");//SearchDate
printf("======5.Deletes the element at the specified location======\n");//DeleteDate
printf("======7.Linear table merging=======\n");//Union
printf("======8.Empty linear table=======\n");//IniList
printf("======0.Exit program======\n");
printf("===================================\n");
printf("\n Please enter the appropriate number for this procedure:");
scanf_s("%d", &i);
switch (i) {
case 1://Assign values to linear tables
IniList(&L1,"1");
IniList(&L2,"2");
IniList(&L_Merge,"_Merge");//Initialize the three tables and assign values to table L1
scanf_s("%d", &L1.length);
input(&L1, L1.length);
system("pause");
system("cls");
goto start;
case 2://View table
Display(&L1);
if (&L2.length != 0) {
printf("\n");
Display(&L2);
}
if (&L_Merge.length != 0) {
printf("\n");
Display(&L_Merge);
}
system("pause");
system("cls");
goto start;
case 3://Insert element
printf("Please enter the name of the table you want to insert data into( L1，L2,L_Merge): ");
system("pause");
system("cls");
goto start;
case 4:
printf("Please enter the data of which table to find(L1,L2,L_Merge;Be case sensitive): ");
system("pause");
system("cls");
goto start;
case 5:
printf("Please enter the data in which table you want to delete(L1,L2,L_Merge;Be case sensitive): ");
system("pause");
system("cls");
goto start;
scanf_s("%d", &L2.length);
system("pause");
system("cls");
goto start;
case 7:
ListMerge(&L1, &L2, &L_Merge);
system("pause");
system("cls");
goto start;
case 8:
IniList(&L1, "1");
IniList(&L2, "2");
IniList(&L_Merge, "_Merge");
system("pause");
system("cls");
goto start;
case 0:
printf("Are you sure to exit the program?(yes or no): ");
system("pause");
system("cls");
goto start;
}
}
}
return 0;
}```

Full code:

```#include<stdio.h>
#include<iostream>
#include<string.h>
#include<malloc.h>
using namespace std;

#define SIZE 100
typedef int ElemType;
typedef int status;

typedef struct sqlist {
ElemType* elem;//Address for storing linear table
string name;//Give a name to the linear table, which is convenient for subsequent output
int length;	   //Length of storage linear table
int listsize;  //Number of linear tables stored
}SqList;//Define linear table
status IniList(SqList* L,string name){
L->elem = (int*)malloc(SIZE*sizeof(ElemType));//Strive for dynamic storage space for L
L->length = 0;//Initialization is to set the length of the linear table to 0
L->listsize = SIZE;//The maximum length of the table is 100. To modify it, see the top
L->name = name;//Give the linear table a name
return 1;//Normal exit
}//Initialization of linear table
void input(SqList* L,int Num){
L->length = Num;//Take the number of input data as the length of the linear table
for (int i = 0; i < L->length; i++)
{
scanf_s("%d", &L->elem[i]);//Assign values to linear tables
}
printf("Input completed!\n");
}//Assign values to linear tables
void Display(SqList* L){
if (L->length == 0) {
return;
}//If the linear table is empty, exit directly
cout << "L" << L->name << ": ";
for (int i = 0; i < L->length; i++) {
printf("%d", L->elem[i]);
printf(" ");
}
printf("\n");
}//Print linear table
status ListInsert(SqList* L) {
int pos;//Define the abbreviation pos of position, which is the position where the data is inserted
ElemType* p, * p1, * p2,value;
if (L->length == 0) {
printf("\n This table is empty, unable to insert data, the data insertion program has exited!!!\n");
return 0;
}//Judge whether the table inserted data is empty. If it is empty, exit
printf("\n Please enter the location to insert:");
scanf_s("%d", &pos);
if (pos<1 || pos>L->length+1) {
printf("Error inserting location information!\n");
return 0;
}//Judge whether the position of the inserted value is reasonable. If it is unreasonable, it will be pushed out
printf("Please enter the element to insert:");
scanf_s("%d", &value);
if (L->length >= L->listsize) {
p =(int*)realloc(L->elem, sizeof(ElemType) * (L->listsize + 10));
if (!p) return 0;
L->elem = p;//New address assigned to L
L->listsize += 10;//The size should be adjusted accordingly
} //When the space is insufficient, expand the capacity. This if conditional statement is generally unavailable and can be omitted
p1 = &L->elem[pos - 1];
for (p2 = &L->elem[L->length - 1]; p2 >= p1; p2--) {
*(p2 + 1) = *(p2);//Move back one unit
}
L->elem[pos - 1] = value;
L->length++;
return 1;
}//Insert element
L->length = Num;//Add a linear table whose length = the number of input values
for (int i = 0; i < L->length; i++){
scanf_s("%d", &L->elem[i]);
}
printf("Input completed!\n");
return 1;
status SearchDate(SqList*L){
int pos_search;//Define where to find
while (true) {
printf("Please enter the location you want to find (the length of the table is):%d): ", L->length);
cin >> pos_search;
if (pos_search-1 < 0 || pos_search-1 >= L->length) {
printf("\n The entered position is incorrect! Please re-enter!!\n");
continue;
}
else break;
}//Judge whether the location of finding data is reasonable. If it is reasonable, exit the while loop structure
printf("This value is:%d\n", L->elem[pos_search-1]);//Since the subscript starts from 0, we need pos_search-1 to output
return 1;
}//Find data
status DeleteDate(SqList* L) {
int pos_delete;//Define where to delete data
while (true) {
printf("Please enter the location of the data you want to delete (the length of the table is):%d): ", L->length);
cin >> pos_delete;
if (pos_delete-1<0 || pos_delete-1>=L->length) {
printf("\n The entered position is incorrect! Please re-enter!!\n");
continue;
}
else break;
}//Judge whether the location of the deleted data is reasonable. If it is reasonable, exit the while loop structure
int date_delete = L->elem[pos_delete-1];//Since the subscript starts from 0, we need pos_delete-1 to operate
for (int i = pos_delete-1; i <= L->length-1; i++) {
L->elem[i] = L->elem[i + 1];
}//Since the subscript starts from 0, we need pos_delete-1 to operate
L->length = L->length - 1;
printf("Deleted data:%d\n", date_delete);
return 1;
}//Delete data
status ListMerge(SqList* L1, SqList* L2, SqList* L_Merge) {
L_Merge->length = L1->length + L2->length;//Add the length of the linear table to be merged, and then assign it to the length of the merged table
int pos1 = L1->length, pos2 = L2->length;//pos1 and pos2 are defined to facilitate assignment to the consolidated table
for (int i = 0; i < L1->length; i++) {
L_Merge->elem[i] = L1->elem[i];
}
for (int i = L1->length, k = 0; k < L2->length; k++, i++) {
L_Merge->elem[i] = L2->elem[k];
}/*The condition body of this for loop is critical. The statement int i = L1 - > length uses the length of table L1
Since the angle sign starts from 0, the length of L1 is directly used as the angle sign to start the value*/
IniList(L1,"1");
IniList(L2,"2");//After merging tables, initialize the two tables (equivalent to destroying linear tables)
printf("Merge complete!\n");
return 1;
}//Linear table merging
status DeleteList(SqList* L1, SqList* L2, SqList* L_Merge) {
IniList(L1,"1");
IniList(L2,"2");
IniList(L_Merge,"_Merge");
printf("Destroy linear table completed!\n");
return 1;
}//The essence of destroying a linear table is to reinitialize the linear table. At this time, the length of each table is 0, so it has been destroyed
int main(){
SqList L1, L2, L_Merge;//Define three tables
int i=1;
while (true) {
start:
printf("===================================\n");
printf("======1.input data======\n");//input
printf("======2.output data======\n");//Display
printf("======3.insert data======\n");//ListInsert
printf("======4.Finds the element at the specified location======\n");//SearchDate
printf("======5.Deletes the element at the specified location======\n");//DeleteDate
printf("======7.Linear table merging=======\n");//Union
printf("======8.Empty linear table=======\n");//IniList
printf("======0.Exit program======\n");
printf("Student No.: 204010110     Name: Cheng Lanchang   \n");
printf("===================================\n");
printf("\n Please enter the appropriate number for this procedure:");
scanf_s("%d", &i);
switch (i) {
case 1://Assign values to linear tables
IniList(&L1,"1");
IniList(&L2,"2");
IniList(&L_Merge,"_Merge");//Initialize the three tables and assign values to table L1
scanf_s("%d", &L1.length);
input(&L1, L1.length);
system("pause");
system("cls");
goto start;
case 2://View table
Display(&L1);
if (&L2.length != 0) {
printf("\n");
Display(&L2);
}
if (&L_Merge.length != 0) {
printf("\n");
Display(&L_Merge);
}
system("pause");
system("cls");
goto start;
case 3://Insert element
printf("Please enter the name of the table you want to insert data into( L1，L2,L_Merge): ");
system("pause");
system("cls");
goto start;
case 4:
printf("Please enter the data of which table to find(L1,L2,L_Merge;Be case sensitive): ");
system("pause");
system("cls");
goto start;
case 5:
printf("Please enter the data in which table you want to delete(L1,L2,L_Merge;Be case sensitive): ");
system("pause");
system("cls");
goto start;
scanf_s("%d", &L2.length);
system("pause");
system("cls");
goto start;
case 7:
ListMerge(&L1, &L2, &L_Merge);
system("pause");
system("cls");
goto start;
case 8:
IniList(&L1, "1");
IniList(&L2, "2");
IniList(&L_Merge, "_Merge");
system("pause");
system("cls");
goto start;
case 0:
printf("Are you sure to exit the program?(yes or no): ");