Sequence table and its basic operations

Detailed sequence table

The sequence table stores the sequence in which the logical relationship is "one-to-one". Before storing data, apply for a physical space of sufficient size in advance.

Sequence table initialization

Before using the sequence table to store data, in addition to applying for a sufficient size of physical space, in order to facilitate the later use of the data in the table, the sequence table also needs to record the following two data in real time: 1. The storage capacity requested by the sequence table; 2.
The
sequence The length of the table, that is, the number of data elements stored in the table;
therefore we need to customize the sequence table, the C language code is as follows:


typedef int ElemType;

typedef struct seqlist {
    
    
	ElemType *elem;		//声明了一个大小不确定的数组,也成为“动态数组”
	int length;			//记录顺序表数据当前大小
}seqlist;

We use a macro to define the size of the application space

#define MAXSIZE	10		//对MAXSIZE进行宏定义,表示顺序表申请空间的大小
#define FALSE 0			//后续会用到,不必理解
#define TRUE 1			//后续会用到,不必理解

typedef unsigned char boolean;		

The following is the initialization sequence table initialization

boolean initList(seqlist* List1) {
    
    		//返回值为boolean类型的一个函数
    List1->elem = (int*)malloc(MAXSIZE * sizeof(int));   //构造一个空的顺序表,动态申请存储空间
    if (!List1->elem) {
    
                                      //如果申请失败,作出提示并直接退出程序
            printf("初始化失败");
            return FALSE;			//前面进行的宏定义,符合返回值类型,看起来会很方便	
    }
    List1->length = 0;//空表的长度初始化为0
    return TRUE;
}

The above is to initialize the sequence table and apply for space for the sequence table.
The following is the processing of the sequence table

Sequence table insert element

By traversing the sequence table, find the data insertion position, and then do the following two steps

  • Move all the elements behind the element to be inserted to the back as a whole
  • Put the element into the position
    For example, in the {1, 2, 3, 4, 5} sequence table, insert element 6 at the position of 4, just move 5 and 6 backwards, and then place 6 at the position of 4.Find the target element 4, move itself and the following elements back, and then put 6 in the position of 4
//插入函数,其中,elem为插入的元素,add为插入到顺序表的位置
seqlist addseqlist(seqlist t,int elem,int add)
{
    
    
    //判断插入本身是否存在问题(如果插入元素位置比整张表的长度+1还大(如果相等,是尾随的情况),或者插入的位置本身不存在,程序作为提示并自动退出)
    if (add > seqlist.length + 1||add < 1) {
    
    
        printf("插入位置有问题");
        return seqlist;
    }
    //插入操作,需要将从插入位置开始的后续元素,逐个后移
    for (int i = seqlist.length - 1; i >= add - 1; i--) {
    
    
        seqlist.elem[i+1]=seqlist.elem[i];
    }
    //后移完成后,直接将所需插入元素,添加到顺序表的相应位置
    seqlist.elem[add-1]=elem;
    //由于添加了元素,所以长度+1
    seqlist.length++;
    return seqlist;
}

The above realizes the insertion of elements in the sequence table. It must be remembered that after inserting elements, the length of the sequence table must be increased by one.

output element

Traverse and output the elements in the sequence table

void showelem(const seqlist *List1) {
    
    
    int i;

    for (i = 0; i < List1->length; i++) {
    
    
        printf("%d ", List1->elem[i]);
    }
    printf("\n");
}

Delete a section of elements in the sequence table

Move the element behind the element to be deleted in the sequence table forward, that is, overwrite the deleted element. To delete an element, let the values ​​of i and j be the same.

boolean DeleteList(seqlist *List1, int i, int j) {
    
    
    if (i < 0 || j < 0 || i > MAXSIZE || j >= MAXSIZE) {
    
    
        printf("删除区域有误!\n");
        return FALSE;
    }
    for (j; j < MAXSIZE; j++) {
    
    
        List1->elem[i - 1] = List1->elem[j];	//将被删除这段元素的下一个元素,挪动到删除的第一个元素处,以此类推
        i++;
    }
    List1->length = MAXSIZE - (j - i) - 1;		//删除元素后长度的大小
	showelem(List1);		//遍历输出删除后的元素
    return TRUE;
}

Lookup of sequence table elements

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

Sequence table change element

The implementation process of the sequence table changing element is as follows:

  1. Find the element to be changed;
  2. Modify the value of the element directly.

The code is implemented as follows:

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

The basic operation of the linear table ends here. If you have any questions, please click the avatar to contact.

Guess you like

Origin blog.csdn.net/adaizzz/article/details/104748838