Basic operation of sequence table and C language implementation (detailed version)

We have learned about the sequence table and the process of initialization. In this section, we learn some basic operations about the sequence table and how to implement them in the C language.

1. Insert elements in the sequence table

Inserting data elements into the existing sequence table can be divided into the following three situations according to the different insertion positions:

  1. Insert into the header of the sequence table;
  2. Insert an element in the middle of the table;
  3. There is an element in the trailing sequence list as the last element in the sequence list;

Although the positions of the data elements in the insertion order table are different, they all use the same method to solve the problem, that is: through traversal, find the position where the data elements are to be inserted, and then do the following two steps:

  • Move the element to be inserted at the position and the subsequent elements one position backward as a whole;
  • put the element in the vacated position;

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

  • Traverse to where the sequence table stores the third data element, as shown in Figure 1:

Figure 1 Find the target element position

  • Move element 3 and subsequent elements 4 and 5 back one position as a whole, as shown in Figure 2:

Figure 2 Freeing up the insertion position

  • Place the new element 6 in the vacated position, as shown in Figure 3:

Figure 3 Insert target element

Therefore, the C language implementation code for inserting data elements in a sequence table is as follows:

//插入函数,其中,elem为插入的元素,add为插入到顺序表的位置
table addTable(table t, int elem, int add)
{
    int i;
    //判断插入本身是否存在问题(如果插入元素位置比整张表的长度+1还大(如果相等,是尾随的情况),或者插入的位置本身不存在,程序作为提示并自动退出)
    if (add > t.length + 1 || add < 1) {
        printf("插入位置有问题");
        return t;
    }
    //做插入操作时,首先需要看顺序表是否有多余的存储空间提供给插入的元素,如果没有,需要申请
    if (t.length == t.size) {
        t.head = (int *)realloc(t.head, (t.size + 1) * sizeof(int));
        if (!t.head) {
            printf("存储分配失败");
            return t;
        }
        t.size += 1;
    }
    //插入操作,需要将从插入位置开始的后续元素,逐个后移
    for (i = t.length - 1; i >= add - 1; i--) {
        t.head[i + 1] = t.head[i];
    }
    //后移完成后,直接将所需插入元素,添加到顺序表的相应位置
    t.head[add - 1] = elem;
    //由于添加了元素,所以长度+1
    t.length++;
    return t;
}

Note that the realloc function is used to apply more physical space for dynamic arrays. Moreover, in the process of realizing the overall backward movement of subsequent elements, the target position actually has data, or 3, but the old element will be directly overwritten when the new element is inserted in the next step.

2. Delete elements from the sequence table

It is very simple to delete the specified element from the sequence table, just find the target element and move all its subsequent elements forward by 1 position as a whole.

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 the element.

For example {1,2,3,4,5} , the process of removing element 3 from it is shown in Figure 4:

Figure 4 Schematic diagram of the process of deleting elements in the sequence table

Therefore, the C language implementation code for deleting an element in a sequence table is:

table delTable(table t, int add) {
    int i;
    if (add > t.length || add < 1) {
        printf("被删除元素的位置有误");
        exit(0);
    }
    //删除操作
    for (i = add; i < t.length; i++) {
        t.head[i - 1] = t.head[i];
    }
    t.length--;
    return t;
}

Third, the sequence table to find elements

Searching for the target element in the sequence table can be implemented using a variety of search algorithms, such as binary search algorithm, interpolation search algorithm, etc.

Here, we choose the sequential search algorithm, and the specific implementation code is:

//查找函数,其中,elem表示要查找的数据元素的值
int selectTable(table t, int elem) {
    int i;
    for (i = 0; i < t.length; i++) {
        if (t.head[i] == elem) {
            return i + 1;
        }
    }
    return -1;//如果查找失败,返回-1
}

Fourth, the sequence table changes elements

The implementation process of the sequence table change element is:

  1. find the target element;
  2. directly modify the value of the element;

The C language implementation code for changing the element of the sequence table is:

//更改函数,其中,elem为要更改的元素,newElem为新的数据元素
table amendTable(table t, int elem, int newElem) {
    int add = selectTable(t, elem);
    t.head[add - 1] = newElem;//由于返回的是元素在顺序表中的位置,所以-1就是该元素在数组中的下标
    return t;
}

The above are the most commonly used basic operations in the process of using the sequence table. Here is the complete implementation code of this section:

#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)
{
    int i;
    if (add > t.length + 1 || add < 1) {
        printf("插入位置有问题");
        return t;
    }
    if (t.length >= t.size) {
        t.head = (int *)realloc(t.head, (t.size + 1) * sizeof(int));
        if (!t.head) {
            printf("存储分配失败");
        }
        t.size += 1;
    }
    for (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) {
    int i;
    if (add > t.length || add < 1) {
        printf("被删除元素的位置有误");
        exit(0);
    }
    for (i = add; i < t.length; i++) {
        t.head[i - 1] = t.head[i];
    }
    t.length--;
    return t;
}
int selectTable(table t, int elem) {
    int i;
    for (i = 0; i < t.length; i++) {
        if (t.head[i] == elem) {
            return i + 1;
        }
    }
    return -1;
}
table amendTable(table t, int elem, int newElem) {
    int add = selectTable(t, elem);
    t.head[add - 1] = newElem;
    return t;
}
void displayTable(table t) {
    int i;
    for (i = 0; i < t.length; i++) {
        printf("%d ", t.head[i]);
    }
    printf("\n");
}
int main() {
    int i, add;
    table t1 = initTable();
    for (i = 1; i <= Size; i++) {
        t1.head[i - 1] = i;
        t1.length++;
    }
    printf("原顺序表:\n");
    displayTable(t1);
    printf("删除元素1:\n");
    t1 = delTable(t1, 1);
    displayTable(t1);
    printf("在第2的位置插入元素5:\n");
    t1 = addTable(t1, 5, 2);
    displayTable(t1);
    printf("查找元素3的位置:\n");
    add = selectTable(t1, 3);
    printf("%d\n", add);
    printf("将元素3改为6:\n");
    t1 = amendTable(t1, 3, 6);
    displayTable(t1);
    return 0;
}

The result of running the program is:

Original sequence table:
1 2 3 4 5
Delete element 1:
2 3 4 5
Insert element at the second position 5:
2 5 3 4 5
Find the position of element 3:
3
Change element 3 to 6:
2 5 6 4 5

Guess you like

Origin blog.csdn.net/qq_38082146/article/details/123637174