# Sequential storage of linear list in C language

Time：2022-6-6

## Implementation of sequential storage of linear list

Time complexity analysis of linear table query, insert and delete for sequential storage structure

Inserting, deleting, querying and deriving by element value are the same as the result

Query by bit o (1)

Code implementation of linear table based on sequential storage

``````#include <stdlib.h>
#include <stdio.h>

#define Elemtype int
#define Init_num 20
#define Init_cur 10
#define Init_value 0

//Basic structure of linear list sequential storage (different from array mode)
//The data area will get continuous storage space, which can be accessed directly using subscripts (different from the linked list, do not think of the linked list when you see the pointer)
typedef struct {
Elemtype *data;// (pointers are used only for dynamic memory space allocation, not pointer access)
int current_max_length;
int list_max_length;
}linear_list;

void initial_ List (linear_list &l) // initialization method of sequential storage structure
{
int i = 0;
L.data = (Elemtype*)malloc(sizeof(Elemtype)*Init_num-1);
for (i=0;i<Init_num;i++)
{
L.data[i] = Init_value;
}
//The operations in the sequence table are counted from zero, so the subscript is one less
L.list_max_length = Init_num-1;
L.current_ max_ length = Init_ cur-1;// Subscript representing the last element that can be manipulated directly
}

int Located_element(linear_list L,Elemtype value)
{
for (int i=0;i<=L.current_max_length;i++)
{
if (value == L.data[i])
{
printf("\nsearched element in location %d\n",i+1);
return i;
break;
}
}
printf("\nsearching operation fail,list did not have this element\n");
return -1;// Value does not exist in the sequence table
}

void delete_element(linear_list &L,int location)
{
//The delete function can only be used if the sequence table is not empty
int temp_loc = location-1;
if(L.current_max_length >= 0)
{
for(temp_loc;temp_loc+1 <= L.current_max_length;temp_loc++)
{
L.data[temp_loc] = L.data[temp_loc+1];
}
L.current_max_length = L.current_max_length-1;
printf("delete operation succeed!\n");
for(int i=0 ;i <= L.current_max_length ; i++)
{
printf("%d ",L.data[i]);
}
printf("\ndone!\n");
}
else
{
printf("delete operation failed,linear list is empty!\n");
}
}

//The destroy operation releases the memory space pointed to by the ElemType type pointer in the structure, rather than releasing the entire structure L
void release_list(linear_list &L)
{
//The free operation is valid only if it is not empty
if(L.data != NULL)
{
free(L.data);
L.data= NULL;
L.current_max_length = -1;
L.list_max_length = -1;
printf("linear list has been released!\n");
}
else
{
printf("pointer is no defeined！\n");
}

}

void insert_element(linear_list &L,Elemtype new_element,int location)
{
//The sequence table has no extra space or is entered illegally
location = location -1;// Start counting with 0 in the sequential table, and start counting with 1 in the logical structure of the linear table
if(L.list_max_length == L.current_max_length || location < 0)
{
printf("insert operation fail!\nno extra space\n");
}
else if (L.list_max_length > L.current_max_length)
{
//Deal with two types of situations
//state_ 1 add directly at the end of the sequence table
if(location == L.current_max_length+1)
{
L.data[location] = new_element;
}

//state_ 2 insert within 0-n of sequence table
else if (location <= L.current_max_length)
{
int end_marke = L.current_max_length;
for(int i=location;i<end_marke;end_marke--)
{
L.data[end_marke] == L.data[end_marke-1];
}
L.data[location] = new_element;
}
printf("linear_list is added one space!\ninsert operation succeed!\n");
L.current_max_length = L.current_max_length + 1;
for(int i=0 ;i <= L.current_max_length ; i++)
{
printf("%d ",L.data[i]);
}
}
}

int main()
{
bool insert = false;
int loc = -2;
linear_list L;
initial_list(L);

for(int i=0 ;i <= L.current_max_length ; i++)
{
printf("%d ",L.data[i]);
}
printf("\ndone!\n");

insert_element(L,5,8);
loc = Located_element(L,5);
delete_element(L,8);
release_list(L);
release_list(L);
return 0;
}``````