Order table of C + data structure and algorithm

Keywords: C less

1. definition

The sequential storage of a linear table is called a sequential table. Elements are adjacent to each other in logical relationship and memory address; random access is supported, and each element in the table can be accessed through subscripts.

2. Structure definition

#define MaxSize 50
typedef struct {
int data[MaxSize];
int length;
}Sqlist;

3. Related algorithm

1) creation of sequence table

void creat(Sqlist &L)
{
int a;
printf("Total number of linear table elements:\n");
scanf("%d",&a);
printf("Enter each element in turn\n");
for(int i=0;i<a;i++)
{
scanf("%d",&L.data[i]);
L.length++;
}
}

2) insert element at specified location

int insert(Sqlist &L,int i , int e){//Insert an element at a location (1 for success, 0 for failure)
if(i<1||i>L.length+1)//If the insertion position is less than 1 or greater than table length + 1, the position is illegal
return 0;
else if(i>=MaxSize)//Cannot be inserted if the position is greater than or equal to the maximum table length
return 0;
else if (i== L.length+1)
{
L.data[L.length] = e;
L.length ++;
return 1;
}
else {
for(int j = L.length;j>=i;j--)//Move all elements after insertion back
{
    L.data[j]=L.data[j-1];//Note here that array subscripts start at 0
    L.data[i-1] = e;
}
    L.length++;//Table length +1
    return 1;
}
}

3) delete the specified location element

int listdelete(Sqlist &L, int i , int &e){//Success return 1 failure return 0
if(i<1||i>L.length)//If the deletion position is less than 1 or greater than the table length, the position is illegal
return 0;
e = L.data[i-1];
for(int j = i; j<L.length; j++)//Move all elements forward after deleting bits
   L.data[j-1] = L.data[j];
L.length--;//Table length -1
return 1;
}

4) search by value

int find(Sqlist L,int e){
int i;
for (i = 0; i < L.length; i++)
{
   if(e==L.data[i])
   {
     return i+1;
   }
   else 
   {
      return 0;
   }
}
}

4. Complete code

#include <stdio.h>
#include <stdlib.h>
//Sequence table definition
#define MaxSize 50
typedef struct {
int data[MaxSize];
int length;
}Sqlist;
//Declare function first
void creat(Sqlist &L);//Establish linear table
void show(Sqlist L);//Show linear table

//Insert data at specified location
int insert(Sqlist &L,int i , int e){//Insert an element at a location (1 for success, 0 for failure)
if(i<1||i>L.length+1)//If the insertion position is less than 1 or greater than table length + 1, the position is illegal
return 0;
else if(i>=MaxSize)//Cannot be inserted if the position is greater than or equal to the maximum table length
return 0;
else if (i== L.length+1)
{
L.data[L.length] = e;
L.length ++;
return 1;
}
else {
for(int j = L.length;j>=i;j--)//Move all elements after insertion back
{
    L.data[j]=L.data[j-1];//Note here that array subscripts start at 0
    L.data[i-1] = e;
}
    L.length++;//Table length +1
    return 1;
}
}

//Delete the specified location element
int listdelete(Sqlist &L, int i , int &e){//Success return 1 failure return 0
if(i<1||i>L.length)//If the deletion position is less than 1 or greater than the table length, the position is illegal
return 0;
e = L.data[i-1];
for(int j = i; j<L.length; j++)//Move all elements forward after deleting bits
   L.data[j-1] = L.data[j];
L.length--;//Table length -1
return 1;
}


//Find by value, return bit order successfully, return 0 failed
int find(Sqlist L,int e){
int i;
for (i = 0; i < L.length; i++)
{
   if(e==L.data[i])
   {
     return i+1;
   }
   else 
   {
      return 0;
   }
}
}


int main()
{
Sqlist L;
L.length=0;//Initialize linear table
creat(L);//Create linear table
/*
printf("Where to insert elements in the order table: \ n ');
int i,e;
scanf("%d",&i);
printf("Insert element value: \ n ');
scanf("%d",&e);
int j = insert(L,i,e);
if(j == 1)
{
printf("Insert succeeded \ n ');
show(L);//Print linear table elements
}
else
{
printf("Insert failed \ n ');
}

printf("Delete the number of positions \ n ');
int h,k;
scanf("%d",&h);
int temp = listdelete(L,  h ,  k);
if(temp == 1)
{
printf("Successful \n "";
show(L);//Print linear table elements
}
else
{
printf("Failure \n "";
}
*/
//Search by value
int e,temp;
printf("Value to find:\n");
scanf("%d",&e);
temp = find(L,e);
if (temp == 0)
{
printf("fail\n");
}
else
{
    printf("Success\n");
    printf("The order of this element is%d\n",temp);
}
return 0;
}
//Establish
void creat(Sqlist &L)
{
int a;
printf("Total number of linear table elements:\n");
scanf("%d",&a);
printf("Enter each element in turn\n");
for(int i=0;i<a;i++)
{
scanf("%d",&L.data[i]);
L.length++;
}
}
//Printing
void show(Sqlist L)
{
int i;
printf("Current element:\n");
for(i=0;i<L.length;i++)
printf("%d\t",L.data[i]);
printf("\n");
}

5. summary

Sequence table is the most basic and simple data structure. It has obvious advantages (random access, simple algorithm) but has prominent disadvantages, which is not conducive to memory optimization and dynamic processing. It is not suitable for some algorithms with high memory requirements and dynamic processing.

Posted by eyespark on Fri, 06 Dec 2019 00:30:32 -0800