Data structure sequence table

Keywords: C Algorithm data structure

1, Sequential storage structure

Sequential table, full name sequential storage structure, is a kind of linear table.
Linear tables are used to store data with "one-to-one" logical relationship, and sequential tables are no exception.

When storing data in the sequence table, a whole physical space of sufficient size will be applied in advance, and then the data will be stored in turn. There is no gap between data elements during storage.

For example, the sequence table is used to store the set {1,2,3,4,5}, and the final storage state of the data is shown in Figure 1:

From this, we can conclude that the storage structure of "continuously storing data with 'one-to-one' logical relationship into a whole physical space in order" is a sequential storage structure.
By observing the storage state of the data in Figure 1, we can find that the data stored in the sequence table is very close to the array. In fact, the sequence table uses arrays to store data.

2, Initialization of sequence table

Before using the sequence table to store data, define a structure:
1. Apply for physical space of sufficient size;
2. The length of the sequence table, that is, the number of data elements stored in the table;
3. Storage capacity applied in the sequence table;

Generally speaking (the storage capacity applied for in the sequence table should be greater than the number of data elements stored in the table)

typedef struct{
    int *head; //It declares an array with uncertain length called head, also known as "dynamic array"
    int length; //The number of data elements stored in the record table
    int size; //Storage capacity allocated by the record sequence table
}tabel;

#define Size 5
tabel initTable()
{
    tabel t;
    t.head = (int *)malloc(Size*sizeof(int));
    if(!t.head)
    {
        printf("initialization failed");
        exit(0);
    }
    t.length = 0;
    t.size = Size;
    return t;
}

Operation results:

3, Sequence table insert element

1. Determine whether there is a problem with the insertion
2. When performing the insertion operation, you first need to check whether the sequence table has excess storage space for the inserted elements. If not, you need to apply
3. During the insertion operation, the subsequent elements from the insertion position need to be moved back one by one
4. After moving backward, directly add the required inserted elements to the corresponding positions in the sequence table

For example, insert element 6 at the third position of {1,2,3,4,5}. The implementation process is as follows:

//Insert function, where place is the position inserted into the sequence table and data is the inserted element
tabel addTabel(tabel t,int place,int data)
{
    //Determine whether there is a problem with the insertion
    if(place>t.length+1 ||place <0 )
    {
        printf("There is a problem with the insertion position\n");
        return t;
    }
    //When inserting, you first need to check whether the sequence table has extra storage space for the inserted elements. If not, you need to apply
    if(t.length >= t.size)
    {
        t.head = (int *)realloc(t.head,(t.size+1)*sizeof(int));
        if(t.head == NULL)
        {
            printf("Storage allocation failed\n");
            return t;
        }
        t.size++;
    }
    //During the insertion operation, the subsequent elements from the insertion position need to be moved back one by one
    for(int i=t.length-1;i>=place-1;i--)
    {
        t.head[i+1] = t.head[i];
    }
    //After the backward movement is completed, the required insertion elements are directly added to the corresponding positions in the sequence table
    t.head[place-1] = data;
    t.length++; 
    return t;
}

Operation results:

4, Sequence table delete element

1. Judge whether the position of the deleted element is correct
2. Delete

It is very simple to delete the specified element from the sequence table. Just find the target element and move all subsequent elements forward one position.
If the subsequent elements move forward one position as a whole, the target element will be deleted directly, which can indirectly achieve the purpose of deleting elements

// Sequence table delete element
tabel delTabel(tabel t,int place)
{
    //1. Judge whether the position of the deleted element is correct
    if(place>t.length || place<0)
    {
        printf("The position of the deleted element is wrong!\n");
        return t;
    }
    //2. Delete
    for(int i=place;i<t.length;i++)
    {
        t.head[i-1] = t.head[i];
    }
    t.length --;
    return t;
}

5, The sequence table looks up the element and returns the element position

//Lookup function, where data represents the value of the data element to be searched
//If the lookup is successful, the location of the element is returned
int findTabel(tabel t,int data)
{
    for(int i=0;i<t.length;i++)
    {
        if(t.head[i] == data)
        {
            return i+1;
        }
    }
    return -1;//If the lookup fails, - 1 is returned
}

6, Change elements in order

//The implementation process of sequence table change elements is as follows:
//1. Find the target element;
//2. Directly modify the value of this element;

tabel changeTabel(tabel t,int data,int newdata)
{

    int place;//Define the location of the element to be modified. Find the element first
    place = findTabel(t,data);
    t.head[place-1] = newdata;//Since the position of the element in the sequence table is returned, place-1 is the subscript of the element in the array
    return t;
}

7, Code for complete sequence table

#include <stdio.h>
#include <stdlib.h>
//Initialization of sequence table

// Define a structure before using a sequential table to store data
// 1. In addition to applying for physical space of sufficient size,
// 2. The length of the sequence table, that is, the number of data elements stored in the table;
// 3. Storage capacity applied in the sequence table;

//(Note: under normal conditions, the storage capacity requested by the sequence table should be greater than the length of the sequence table.)
typedef struct{
    int *head; //It declares an array with uncertain length called head, also known as "dynamic array"
    int length; //The number of data elements stored in the record table
    int size; //Storage capacity allocated by the record sequence table
}tabel;

#define Size 5
tabel initTable()
{
    tabel t;
    t.head = (int *)malloc(Size*sizeof(int));
    if(!t.head)
    {
        printf("initialization failed");
        exit(0);
    }
    t.length = 0;
    t.size = Size;
    return t;
}

//Insert function, where place is the position inserted into the sequence table and data is the inserted element
tabel addTabel(tabel t,int place,int data)
{
    //Determine whether there is a problem with the insertion
    if(place>t.length+1 ||place <0 )
    {
        printf("There is a problem with the insertion position\n");
        return t;
    }
    //When inserting, you first need to check whether the sequence table has extra storage space for the inserted elements. If not, you need to apply
    if(t.length >= t.size)
    {
        t.head = (int *)realloc(t.head,(t.size+1)*sizeof(int));
        if(t.head == NULL)
        {
            printf("Storage allocation failed\n");
            return t;
        }
        t.size++;
    }
    //During the insertion operation, the subsequent elements from the insertion position need to be moved back one by one
    for(int i=t.length-1;i>=place-1;i--)
    {
        t.head[i+1] = t.head[i];
    }
    //After the backward movement is completed, the required insertion elements are directly added to the corresponding positions in the sequence table
    t.head[place-1] = data;
    t.length++; 
    return t;
}

// Sequence table delete element
tabel delTabel(tabel t,int place)
{
    //1. Judge whether the position of the deleted element is correct
    if(place>t.length || place<0)
    {
        printf("The position of the deleted element is wrong!\n");
        return t;
    }
    //2. Delete
    for(int i=place;i<t.length;i++)
    {
        t.head[i-1] = t.head[i];
    }
    t.length --;
    return t;
}

//Lookup function, where data represents the value of the data element to be searched
//If the lookup is successful, the location of the element is returned
int findTabel(tabel t,int data)
{
    for(int i=0;i<t.length;i++)
    {
        if(t.head[i] == data)
        {
            return i+1;
        }
    }
    return -1;//If the lookup fails, - 1 is returned
}

// The implementation process of sequence table change elements is as follows:
// 1. Find the target element;
// 2. Directly modify the value of this element;
tabel changeTabel(tabel t,int data,int newdata)
{

    int place;//Define the location of the element to be modified. Find the element first
    place = findTabel(t,data);
    t.head[place-1] = newdata;//Since the position of the element in the sequence table is returned, place-1 is the subscript of the element in the array
    return t;
}

void displayTable(tabel t)
{
    for(int i=0;i<t.length;i++)
    {
        printf("%d ",t.head[i]);
    }
    printf("\n");
}

int main()
{
    tabel t;
    int i;
    t = initTable();
    //Adds an element group to the sequence table
    for(i=0;i<Size;i++)
    {
        t.head[i] = i+1;
        t.length++;
    }
    printf("The elements stored in the sequence table are:\n");
    displayTable(t);

    //Add sequential table elements
    printf("Insert element 6 at position 3 in the sequence table\n");
    t = addTabel(t,3,6);
    displayTable(t);

    //Delete elements from the sequence table
    printf("Delete the element at position 3:\n");
    t = delTabel(t,3);
    displayTable(t);

    //lookup
    printf("Find the location of element 3:%d\n",findTabel(t,3));
    
    //Change element
    printf("Change element 4 to element 6\n");
    t = changeTabel(t,4,6);
    displayTable(t);

    return 0;
}


Posted by atdawgie on Fri, 19 Nov 2021 11:17:05 -0800