C/C++ data structure---sequence table---linear storage structure

Homepage:

There are still unknowns waiting to be explored_Small projects, Luogu quizzes, data structure-CSDN Blog

Topic columns---data structure:

Data Structure_There are still unknown blogs waiting to be explored-CSDN Blog

Table of contents

1. Knowledge reserve

2. References

 3. Sequence table

The first step is to create a sequence table type

The second step is to define and initialize the sequence table  

 The third step, basic operations of sequence tables

1. Insert (increase)

2.Delete

3. take

4.Check

5. Calculate the length of the linked list

4. Complete sequence list


1. Knowledge reserve

        The high-level languages ​​we learned before, such as C language and JAVA language, talked about some grammar, sequential structure, selection structure, loop structure, and how to write code to solve some problems through those three structures and grammar.

        Now, data structure is studying the logical structure of data and the physical structure of data and the relationship between them, defining appropriate operations for this structure, designing corresponding algorithms, and ensuring that after these operations The resulting new structure still maintains the original structure type.

        Data structures can be roughly divided into three categories: linear tables, trees, and graphs. These three categories basically have sequential storage structures and chain storage structures. This article is mainly about the sequential storage structure of sequential tables in linear tables .

2. References

The sequential storage structure of a sequential table means that all the data elements of the linear table are stored in a group of consecutive memory units in their logical order. The linear table stored in this storage form becomes a sequential table. Its characteristic is that two data elements that are logically adjacent are also physically adjacent .

student information sheet

Student number name score 1 2022123 Zhang San 90 2 2022124 Li Si 95 3 2022125 Wang Wu 85

Just like the student information table above, how would you store this table? Should we directly store this information in a structure that describes people, and then define a structure array for storage?

struct student
{
    int num;//学号
    char name[20];//姓名
    int score;//成绩
};
#define MAX 3
struct student s[MAX];
for(int i=0;i<MAX;i++)
{
    scanf("%d",&s[i].num);
    char name[20];
    scanf("%s",name);
    strcpy(s[i].name,name);
    scanf("%d",&s[i].score);
}

Will it be stored as above?

 3. Sequence table

The first step is to create a sequence table type

#define MAX 100
typedef struct
{
    int num;
    char name[20];
    int score;
}elemtype;
typedef struct LNode
{
    elemtype data[MAX];
    int last;
}List;

        Everyone should know that define defines a constant MAX and sets its value to 100. Then, all occurrences of 100 in the next section are replaced by MAX. In this way, if you want to modify the number (expand the array), you can directly define the change in define, avoiding the cumbersome steps of modifying the data.

        typedef is a renaming keyword that can rename data types.

        When defining the sequence list structure, the array inside is also defined as a structure type, and all the required data types are placed in it.

The second step is to define and initialize the sequence table  

List* ListInit()
{
    List* L;
    L = (List*)malloc(sizeof(List));
    L->last = -1;
    return L;
}

 Assign L->last to -1, and then set L->last++ when inputting data. This L->last actually stores the subscript of the last element in the array. When the table length is 0, its value is -1.

 The third step, basic operations of sequence tables

1. Insert (increase)

The operation of sequentially adding data to the initialized empty sequence table.

It should be noted that the data filled in the sequence table must be stored continuously, not every other one.

List* ListInsert(List* L)
{
    int P;//插入的位置,从0开始
    //插入的元素
    int num, score;
    char name[20];
    scanf("%d%d", &P, &num);
    scanf("%s", name);
    scanf("%d", &score);
        if (P<0 || P>L->last+1)//大于L->last+1是因为,数据可以插在顺序表的最后一个元素的后面
        {
            printf("坐标非法\n");
        }
        else
        {
            for (int i = L->last; i >= P; i--)
            {
                L->data[i + 1].num = L->data[i].num;
                L->data[i + 1].score = L->data[i].score;
                strcpy(L->data[i + 1].name, L->data[i].name);
            }
            L->data[P].num = num;
            L->data[P].score = score;
            strcpy(L->data[P].name, name);
            L->last++;
        }
}

if (P<0 || P>L->last+1)//大于L->last+1是因为,数据可以插在顺序表的最后一个元素的后面 

This statement is to allow our data to be stored continuously . When P<0, the coordinates are illegal; P>L->last+1 is to connect the inserted data to the original sequence table.

2.Delete

Delete is to delete the data at the specified location.

List* Listdelete(List* L)
{
    int P;//删除位置,从0开始
    scanf("%d", &P);
    if (L->last == -1)
    {
        printf("顺序表为空,无法删除\n");
    }
    else
    {
        if (P<0 || P>L->last)
            printf("坐标非法\n");
        else
        {
            for (int i = P; i < L->last; i++)
            {
                L->data[i].num = L->data[i + 1].num;
                L->data[i].score = L->data[i + 1].score;
                strcpy(L->data[i].name, L->data[i + 1].name);
            }
            L->last--;
        }
    }
    return L;
}

3. take

Get the element at the specified position

int ListGet(List* L)
{
    int P;//取指定位置,从0开始
    scanf("%d", &P);
    if (P<0 || P>L->last)
    {
        printf("坐标非法\n");
        return -1;
    }
    else
    {
        return P;
    }
    
}

4.Check

Find whether there is an element in the sequence table, return the subscript if found, otherwise return -1

int ListSearch(List* L)
{
    int X;//查找的数据
    scanf("%d", &X);
    for (int i = 0; i <= L->last; i++)
    {
        if (X == L->data[i].num)
            return i;
    }
    return -1;
}

5. Calculate the length of the linked list

Calculated by L->last storing the subscript of the last element.

int ListLength(List* L)
{
    return L->last + 1;
}

4. Complete sequence list

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 5
typedef struct
{
    int num;
    char name[20];
    int score;
}elemtype;
typedef struct LNode
{
    elemtype data[MAX];
    int last;
}List;
List* ListInit();
List* ListInsert(List* L);
List* Listdelete(List* L);
int ListGet(List* L);
int ListSearch(List* L);
int ListLength(List* L);
int main()
{
    List* L;
    L = ListInit();
    int n;//插入数据的个数
    scanf("%d", &n);
    while (n--)
    {
        L = ListInsert(L);
        if (L->last == MAX - 1)
        {
            printf("顺序表已满,无法插入数据\n");
            break;
        }
    }
    L = Listdelete(L);
    int s=ListGet(L);
    if (s != -1)
    {
        printf("%d %s %d\n", L->data[s].num, L->data[s].name, L->data[s].score);
    }
    int s=ListSearch(L);
    if (s != -1)
    {
        printf("找到了,下标为%d\n", s);
    }
    else
    {
        printf("未找到\n");
    }
    int len=ListLength(L);
    printf("%d", len);
	return 0;
}
List* ListInit()
{
    List* L;
    L = (List*)malloc(sizeof(List));
    L->last = -1;
    return L;
}
List* ListInsert(List* L)
{
    int P;//插入的位置
    //插入的元素
    int num, score;
    char name[20];
    scanf("%d%d", &P, &num);
    scanf("%s", name);
    scanf("%d", &score);
    if (P<0 || P>L->last+1)//大于L->last+1是因为,数据可以插在顺序表的最后一个元素的后面
    {
        printf("坐标非法\n");
    }
    else
    {
        for (int i = L->last; i >= P; i--)
        {
            L->data[i + 1].num = L->data[i].num;
            L->data[i + 1].score = L->data[i].score;
            strcpy(L->data[i + 1].name, L->data[i].name);
        }
        L->data[P].num = num;
        L->data[P].score = score;
        strcpy(L->data[P].name, name);
        L->last++;
    }
    return L;
}
List* Listdelete(List* L)
{
    int P;//删除位置
    scanf("%d", &P);
    if (L->last == -1)
    {
        printf("顺序表为空,无法删除\n");
    }
    else
    {
        if (P<0 || P>L->last)
            printf("坐标非法\n");
        else
        {
            for (int i = P; i < L->last; i++)
            {
                L->data[i].num = L->data[i + 1].num;
                L->data[i].score = L->data[i + 1].score;
                strcpy(L->data[i].name, L->data[i + 1].name);
            }
            L->last--;
        }
    }
    return L;
}
int ListGet(List* L)
{
    int P;//取指定位置
    scanf("%d", &P);
    if (P<0 || P>L->last)
    {
        printf("坐标非法\n");
        return -1;
    }
    else
    {
        return P;
    }
    
}
int ListSearch(List* L)
{
    int X;//查找的数据
    scanf("%d", &X);
    for (int i = 0; i <= L->last; i++)
    {
        if (X == L->data[i].num)
            return i;
    }
    return -1;
}
int ListLength(List* L)
{
    return L->last + 1;
}

Thank you for your support! ! mutual encouragement!

Guess you like

Origin blog.csdn.net/qq_73435980/article/details/132782286