# Knowledge framework # Definition and basic operation of linear table

## Definition of linear table From this, we conclude that the characteristics of the linear table are as follows:

The number of elements in the table is limited.

The elements in the table have logical order, and the elements in the table have their order.

The elements in the table are data elements, and each element is a single element.

The data types of the elements in the table are the same, which means that each element occupies the same size of storage space.

The elements in the table are abstract, that is, they only discuss the logical relationship between elements without considering what elements represent.

Note: a linear table is a logical structure that represents one-to-one adjacency between elements. Sequential list and linked list refer to storage structures. They belong to different levels of concepts, so don't confuse them.

## Basic operation of linear table

The basic operation of a data structure refers to its core and basic operation. Other more complex operations can be realized by calling their basic operations. The main operations of the linear table are as follows:

Initlist: initialize table. Construct an empty linear table.

Length(L): find the table length. Returns the length of linear table L, that is, the number of data elements in the job.

Locaterelem (L, e): find operation by value. Finds an element with a given key value in the table.

GetElem(L,i): bitwise lookup operation. Gets the value of the element at position i in the table.

Listinsert (& i, e): insert operation. Insert the specified element E at the ith position in the table.

ListDelete & L, i, & e: delete operation. Delete the element at position i in the table and return the value of the deleted element with e.

PrintList(L): output operation. Output all element values of linear table in sequence.

Empty(L): empty judgment. If the work table is empty, return true; otherwise, return false.

Destroylist: destroy operation. Destroy the linear table and free the memory space occupied by linear table X.

Note: ① the implementation of basic operations depends on which storage structure is adopted. Different storage structures lead to different implementation of algorithms. ② "&" indicates a reference call in C + +. If the passed in variable is a needle type variable and the passed in pointer needs to be changed in the function body, the reference type of pointer variable will be used. The same effect can be achieved by using pointers in C. # Sequential representation of linear tables

## Definition of sequence table

Sequential table – a linear table implemented in sequential storage
Sequential storage. Logically adjacent elements are stored in storage units that are also adjacent in physical locations, and the relationship between elements is reflected by the adjacency relationship of storage units. ## Implementation of sequence table ### Implementation of sequence table - static allocation

```#include <stdio.h>

#define MAXSIZE 10

struct SqList{

//Sequence table
int data[MAXSIZE];

//Sequence table length
int length;
};

typedef struct  SqList SqList;

/*
Initialization sequence table
*/
void initList(SqList *list){

int i=0;

//The length of the initialization sequence table is 5
(*list).length=5;

//Insert 5 elements into the sequence table
for(;i<5;i++){
(*list).data[i]=i+1;
}
}

/*
Insert: inserts an element at a specified position in the sequence table
SqList *list: Sequence table
int index: position
int element: element

Returns an int variable:
1--Insert successful
0--Insert failed
*/
int insertList(SqList *list,int index,int element){

int tmp=(*list).length-1;
int i=index-1;

//If the insertion position is greater than the length of the sequence table, 0 is returned
if(index>(*list).length){
return 0;
}

//Judge whether the space of the sequence table is full
if((*list).length>=sizeof((*list).data)/sizeof(int)){
return 0;
}

//Insert element
for(;tmp>=i;tmp--){
(*list).data[tmp+1]=(*list).data[tmp];
}

(*list).data[i]=element;

(*list).length=(*list).length+1;

return 1;
}

/*
Delete: deletes the element at the specified position in the sequence table
SqList *list: Sequence table
int index: position
int *element: Deleted element

Returns an int variable:
1--Insert successful
0--Insert failed
*/
int deleteList(SqList *list,int index,int *element){

int tmp=(*list).length-1;
int i=index-1;

//If the deletion position is greater than the length of the sequence table, 0 is returned
if(index>(*list).length){
return 0;
}

//If the deletion position is less than 1, return 0
if(index<1){
return 0;
}

*element=(*list).data[index-1];

//Delete element
for(;i<=tmp;i++){
(*list).data[i]=(*list).data[i+1];
}

(*list).length=tmp;

return 1;
}

/*
Bitwise lookup: returns the elements of a sequential table by position
SqList list: Sequence table
int index: position

Returns an int variable:
index Element at position
*/
int getElementByIndex(SqList list,int index){
return list.data[index-1];
}

/*
Find by value:
SqList list: Sequence table
int element: element

Returns an int variable:
index Position: Return - 1, indicating that the sequence table does not have this element
*/
int getIndexByElement(SqList list,int element){
int i=0;
int index=-1;
for(;i<list.length;i++){
if(list.data[i]==element){
index=i+1;
break;
}
}

return index;
}

int main(){

SqList list;

int i=0;

int j=0;

int element=0;

initList(&list);

insertList(&list,3,8);

deleteList(&list,3,&element);

for(;j<list.length;j++){
printf("%d",list.data[j]);
}

return 0;

}
```

### Implementation of sequence table - dynamic allocation

```#include <stdio.h>

#include <stdlib.h>

//Initialization length of sequence table
#define initSize 10

struct SeqList{

int *data;

//Maximum capacity of the array
int maxSize;

//Length of sequence table
int length;
};

typedef  struct SeqList SeqList;

/*
Initialization sequence table:
The maximum capacity is 10
Length 5
The sequence table elements are 1 to 5
*/
void  initList(SeqList *list){

int i=0;

//Set maximum capacity
(*list).maxSize=initSize;

//Set the sequence table length to 5
(*list).length=initSize-5;

//Dynamically allocate memory for sequential tables
(*list).data=(int *)malloc(sizeof(int)*initSize);

for(;i<(*list).length;i++){
(*list).data[i]=i+1;
}
}

/*
Dynamic capacity expansion of sequence table:
SeqList *list: Sequence table
int len: Expansion length
*/
void increaseSize(SeqList *list,int len){

int i=0;

int *tmp=(*list).data;

//Maximum capacity expansion
(*list).maxSize=(*list).maxSize+len;

//Reallocate memory dynamically for linear tables
(*list).data=(int *)malloc(sizeof(int)*(*list).maxSize);

//Original element migration
for(;i<(*list).length;i++){
(*list).data[i]=tmp[i];
}

free(tmp);
}

/*
Insert: inserts an element at a specified position in the sequence table
SeqList *list: Sequence table
int index: position
int element: element

Returns an int variable:
1--Insert successful
0--Insert failed
*/
int insertList(SeqList *list,int index,int element){

int tmp=(*list).length-1;
int i=index-1;

//If the insertion position is greater than the length of the sequence table, 0 is returned
if(index>(*list).length){
return 0;
}

//Judge whether the space of the sequence table is full
if((*list).length>=(*list).maxSize){
return 0;
}

//Insert element
for(;tmp>=i;tmp--){
(*list).data[tmp+1]=(*list).data[tmp];
}

(*list).data[i]=element;

(*list).length=(*list).length+1;

return 1;
}

/*
Delete: deletes the element at the specified position in the sequence table
SeqList *list: Sequence table
int index: position
int *element: Deleted element

Returns an int variable:
1--Insert successful
0--Insert failed
*/
int deleteList(SeqList *list,int index,int *element){

int tmp=(*list).length-1;
int i=index-1;

//If the deletion position is greater than the length of the sequence table, 0 is returned
if(index>(*list).length){
return 0;
}

//If the deletion position is less than 1, return 0
if(index<1){
return 0;
}

*element=(*list).data[index-1];

//Delete element
for(;i<=tmp;i++){
(*list).data[i]=(*list).data[i+1];
}

(*list).length=tmp;

return 1;
}

/*
Bitwise lookup: returns the elements of a sequential table by position
SeqList list: Sequence table
int index: position

Returns an int variable:
index Element at position
*/
int getElementByIndex(SeqList list,int index){
return list.data[index-1];
}

/*
Find by value:
SeqList list: Sequence table
int element: element

Returns an int variable:
index Position: Return - 1, indicating that the sequence table does not have this element
*/
int getIndexByElement(SeqList list,int element){
int i=0;
int index=-1;
for(;i<list.length;i++){
if(list.data[i]==element){
index=i+1;
break;
}
}

return index;
}

int main(){
SeqList list;
int i=0;
int j=0;

initList(&list);

insertList(&list,3,8);

deleteList(&list,3,&j);

for(;i<list.length;i++){
printf("%d",list.data[i]);
}

increaseSize(&list,5);

return 0;
}
```     Posted on Tue, 28 Sep 2021 12:52:36 -0400 by gbow