Sequence table of linear table_initialization_addition, deletion, modification

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right

Article directory


1. Introduction

Sequence table is a basic data structure, its main purpose is to store a set of elements with the same type, and support operations such as fast access, insertion, and deletion of these elements.

Sequence tables are suitable for scenarios where the number of elements is relatively fixed and the number of operations is large, such as scenarios that require frequent operations such as searching, sorting, and statistics. Its advantage is that it has good random access performance, can directly access elements through subscripts, and supports continuous storage, which is beneficial to the effective use of memory space.

Common application scenarios include:

  • Table in the database: each row in the table can be regarded as an element, and each column of the table is the attribute of the element. When performing operations such as data query, sorting, and statistics, the sequence table can be used for storage and operation.
  • Arrays and vectors: Elements in arrays and vectors usually have the same data type and need to support operations such as fast access, modification, insertion, and deletion. In this case, a sequential table can be used as the underlying data structure to improve the efficiency of data operations.
  • Data Structures in Computer Programs: In programs, various data structures are used to store and process data. Sequence tables can be used as the underlying implementation of data structures such as stacks, queues, and heaps, providing efficient data storage and operation capabilities.

In short, as a basic data structure, sequence table has a wide range of application scenarios and plays an important role in the fields of computer science and software engineering.

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

#define Size 5

typedef struct Table {
    
    
    int* head;
    int length;
    int size;
}table;

table initTable() {
    
    
    table t;
    t.head = (int*)malloc(Size * sizeof(int));
    if (!t.head) {
    
    
        printf("初始化失败");
        exit(0);
    }
    t.length = 0;
    t.size = Size;
    return t;
}

table addTable(table t, int elem, int add)
{
    
    
    if (add > t.length + 1 || add < 1) {
    
    
        printf("插入位置有问题\n");
        return t;
    }

    if (t.length == t.size) {
    
    
        int* newHead = (int*)realloc(t.head, (t.size + 1) * sizeof(int));
        if (!newHead) {
    
    
            printf("存储分配失败\n");
            return t;
        }
        t.head = newHead;
        t.size += 1;
    }

    for (int i = t.length - 1; i >= add - 1; i--) {
    
    
        t.head[i + 1] = t.head[i];
    }

    t.head[add - 1] = elem;
    t.length++;
    return t;
}
table delTable(table t, int add) {
    
    
    if (add > t.length || add < 1) {
    
    
        printf("被删除元素的位置有误\n");
        return t;
    }
    //删除操作
    for (int i = add; i < t.length; i++) {
    
    
        t.head[i - 1] = t.head[i];
    }
    t.length--;
    return t;
}
//查找函数,其中,elem表示要查找的数据元素的值,可以使用多种查找算法实现,比如说二分查找算法、插值查找算法
int selectTable(table t, int elem) {
    
    
    for (int i = 0; i < t.length; i++) {
    
    
        if (t.head[i] == elem) {
    
    
            return i + 1;//由于返回的是元素在顺序表中的下标,所以+1就是该元素在数组中的位置是第几个
        }
    }
    return -1;//如果查找失败,返回-1
}
//更改函数,其中,elem为要更改的元素,newElem为新的数据元素
table amendTable(table t, int elem, int newElem) {
    
    
    int add = selectTable(t, elem);
    t.head[add - 1] = newElem;//由于返回的是元素在顺序表中的位置,所以-1就是该元素在数组中的下标
    return t;
}
void displayTable(table t) {
    
    
    for (int i = 0; i < t.length; i++) {
    
    
        printf("%d ", t.head[i]);
    }
    printf("\n");
}

int main() {
    
    
    table t = initTable();
    int a = 5;
    //向顺序表中添加元素
    for (int i = 1; i <= Size; i++) {
    
    
        a = a + 5;
        t.head[i - 1] = a;
        t.length++;
    }

    printf("顺序表中存储的元素分别是:\n");
    displayTable(t);

    //在顺序表中插入元素
    t = addTable(t, 99, 3);
    printf("插入元素后,顺序表中存储的元素分别是:\n");
    displayTable(t);

    //在顺序表删除元素
    t = delTable(t, 2);
    printf("删除元素后,顺序表中存储的元素分别是:\n");
    displayTable(t);
    //在顺序表中查找元素
    int selectnum;
    selectnum = selectTable(t, 99);
    printf("查找到的元素位置是:%d\n", selectnum);
    //改
    amendTable(t, 99, 88);
    printf("改变元素后,顺序表中存储的元素分别是:\n");
    displayTable(t);
    return 0;
}

The elements stored in the sequence table are:
10 15 20 25 30
After the element is inserted, the elements stored in the sequence table are:
10 15 99 20 25 30
After the element is deleted, the elements stored in the sequence table are:
10 99 20 25 30
The position of the found element is: 2
After changing the element, the elements stored in the sequence table are:
10 88 20 25 30

Guess you like

Origin blog.csdn.net/lianghuajunone/article/details/130218167