C language sequence table (sequential storage structure)

Sequence table (sequential storage structure) and the initialization process detailed

Order table , the full name of sequential storage structure , is a linear table. By learning a "linear form" We know that the linear table used to store the logical relationship as "one to one" data, the order table is no exception.

Moreover, the physical order of the table data storage structure is also required. When the sequence table store data, will apply in advance a piece of physical space of sufficient size, the data is sequentially stored, do not leave a trace of a gap between the time the data elements stored.

For example, using sequence table store a set of {1,2,3,4,5}data stored in a final state as shown below:
Here Insert Picture Description
Thus we can conclude that the "having 'one to one' logical relationship of data in the order of successively stored onto a piece of physical space," the memory structure is sequential storage structure.

By storing the status data in FIG observation, we can see that the same data sequence table storage array is very close. In fact, the order table stores data used is the array.

Initialization sequence table

Before using sequence table storage data, in addition to the physical space of sufficient size to apply the outside, in order to facilitate later use the data in the table, the order table also requires real time recording data of the following two:

  1. Application sequence table storage capacity;
  2. The length of the sequence table, i.e. the number of data elements stored in the table;

Note: Under normal conditions, the application of the sequence table storage capacity greater than the length of the sequence table

Therefore, we need to customize the order of the table, C language codes are as follows:

typedef struct Table{
    int * head;//声明了一个名为head的长度不确定的数组,也叫“动态数组”
    int length;//记录当前顺序表的长度
    int size;//记录顺序表分配的存储容量
}table;

Note, head dynamic array is an uninitialized our statement, do not just think of it as a normal pointer.

Then start learning initialization sequence table, which is the initial establishment of a sequence table. Establish order in the table need to do the following:

  • Application head dynamic data to the physical space of sufficient size;
  • Size and length to initial values;
    Thus, C language codes are as follows:
#define Size 5 //对Size进行宏定义,表示顺序表申请空间的大小
table initTable(){
    table t;
    t.head=(int*)malloc(Size*sizeof(int));//构造一个空的顺序表,动态申请存储空间
    if (!t.head) //如果申请失败,作出提示并直接退出程序
    {
        printf("初始化失败");
        exit(0);
    }
    t.length=0;//空表的长度初始化为0
    t.size=Size;//空表的初始存储空间为Size
    return t;
}

We see that the entire sequence table initialization process is encapsulated into a function, the function has a return value is of the form initialization sequence. The benefit of this is to increase the availability of the code, but also more beautiful. At the same time, the order table initialization process, pay attention to the physical space of the application judge on the case of application failure of processed here only had a "prompt information and to force out" operation, according to your own needs code if statement to improve.

By calling initTable statement in the main function, you can successfully create a blank order form, at the same time we can also try to add some elements to the table in order, C language codes are as follows:

#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;
}
//输出顺序表中元素的函数
void displayTable(table t){
    for (int i=0;i<t.length;i++) {
        printf("%d ",t.head[i]);
    }
    printf("\n");
}
int main(){
    table t=initTable();
    //向顺序表中添加元素
    for (int i=1; i<=Size; i++) {
        t.head[i-1]=i;
        t.length++;
    }
    printf("顺序表中存储的元素分别是:\n");
    displayTable(t);
    return 0;
}

Program results are as follows:

顺序表中存储的元素分别是:
1 2 3 4 5

You can see, the order table initialized successfully.

Basic operation sequence table

Order table insert elements

Insert data elements into an existing order in a table, depending on the insertion position, can be divided into the following three cases:

  1. Inserted into the header of the sequence table;
  2. Insert element in an intermediate position of the table;
  3. Trailing elements already in the sequence table, as the last element in the sequence table;

Although the data elements into position order of the table is different, but all use the same way to solve, namely: by traversing to find the location of the data elements to be inserted, and then do the following two steps:

  • The position of the element to be inserted and the subsequent movement of the whole element a rearward position;
  • The element placed on the vacated position;

For example, the {1,2,3,4,5}insertion of the element 6 in three positions, the implementation process is as follows:

  • Traversal order to position the third data table storage elements, as shown below:
    Here Insert Picture Description
    The element 3 and the subsequent elements 4 and 5 integrally move rearward position, as shown in the figure:
    Here Insert Picture Description
  • The new element 6 into the vacated position, as shown below:
    Here Insert Picture Description
    Thus, C language data element inserted sequence table codes are as follows:
//插入函数,其中,elem为插入的元素,add为插入到顺序表的位置
table addTable(table t,int elem,int add)
{
    //判断插入本身是否存在问题(如果插入元素位置比整张表的长度+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 (int 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 dynamic array of additional applications more physical space using realloc function. And, after a subsequent element to achieve the overall shift process, in fact, the target position data, or 3, directly over old elements will only when the next new elements inserted.

Delete element sequence table

Removes the specified element from the table in order to achieve very simple, just find the target element, and all elements of the overall follow-up to a forward position.

后续元素整体前移一个位置,会直接将目标元素删除,可间接实现删除元素的目的。

For example, the {1,2,3,4,5}process of deleting elements 3 as shown below:
Here Insert Picture Description
Thus, the sequence table remove elements of the C language code:

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

Find the order table of elements

The order of the table to find the target element, you can use a variety of search algorithm to achieve, such as binary search algorithm, interpolation search algorithm and so on.

Here, we choose the order of the search algorithm, the specific implementation code as follows:

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

Change the order of elements in the table

Change the order of the table of elements implementation process is:

  1. Find the target element;
  2. Directly modifying the value of the element;

Change the order of the table of elements C language code:

//更改函数,其中,elem为要更改的元素,newElem为新的数据元素
table replaceTable(table t,int elem,int newElem)
{
	int add=searchable(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");
}

Receiving a maximum number of entries and length of the return sequence table

The two elements belonging to the structure, can be directly returned

//返回顺序表的长度
int returnLength(table t)
{
	return t.length;//返回结构体的长度元素即可
}
//返回顺序表最大容纳表项个数
int returnSize(table t)
{
	return t.size;
}

These are the order of the table during use of the most common operations, related to the complete code has been push to GitHub, need a small partner own clone, if you feel good, welcome Star, here is the portal sequence table (sequential storage structure) , in addition to addition to this, the knowledge of children's shoes want to know more C, C ++, Java, Python, etc., welcome to my blog ( reunion blog ), we discussed together! Next, I will continue to update other language sequence table, which I will next time algorithm analysis, when implemented in C ++ to share with you, so stay tuned!

Published 19 original articles · won praise 8 · views 614

Guess you like

Origin blog.csdn.net/qq_43336390/article/details/103958584