Data Structure - Linear sequentially stored table

Linear form of sequential storage

Linear table

线性表作为一种基本的数据结构类型,在计算机存储器中的表示一般有两种:一种是顺序表示,另一种是链式表示。

Sequential storage structure features:

(1)逻辑上相邻的元素其存储位置也是相邻的;
(2)对数据元素的存取是随机存取或者按地址存取;
(3)存储密度高。C存储密度=(数据结构中元素所占的存储空间)/(整个数据结构所占的空间)

Sequential storage structure is insufficient:

对表的插入和删除的等运算的时间复杂度较差。
![算法的时间复杂度](https://img-blog.csdnimg.cn/20190606194732905.png)!

Defined sequence table

在C语言中,一位数组的元素也是存放在一片连续的存储空间中,故可以借助C语言中一位数组类型来描述顺序表的存储结构。
#define SIZE 100 //定义顺序表的长
typedef int data_t;   //方便以后修改表中存储数据的数据类型
typedef struct seq_list{
    data_t list[SIZE];  //顺序表
    data_t last;       //指向表尾元素的指针
}sqlist;

Order table related operations

sqlist * CreateSqlist (); // Create a table

sqlist *CreateSqlist(){
	sqlist * sq = (sqlist *)malloc(sizeof(sqlist));
	if(sq == NULL)
		return NULL;
	memset(sq->list,0,sizeof(sq->list));
	sq->last = -1;
	return sq;
}

int ClearSqlist (sqlist * sq); // Empty Table

int ClearSqlist(sqlist *sq){
	if(sq == NULL){
		return -1;
	}
	sq->last = -1;  //将表尾置为-1
	return 0;
}

int Sqlist_is_empty (sqlist * sq); // determines whether the table is empty

int Sqlist_is_empty(sqlist *sq){
	if(sq == NULL)
		return -1;
	return sq->last == -1;  //如果为-1即为空
}

int Sqlist_is_full (sqlist * sq); // determine whether the table is full

int Sqlist_is_full(sqlist *sq){
	if(sq == NULL)
		return -1;
	return SIZE - 1 == sq->last;
}

int GetLengthSqlist (sqlist * sq); // find the number of elements in the table

int GetLengthSqlist(sqlist *sq){
	if(sq == NULL)
		return -1;
	return sq->last+1;
}

int InsertSqlistBypos (sqlist * sq, int pos, data_t data); // element into position by

int InsertSqlistBypos(sqlist *sq,int pos,data_t data){
	if(sq == NULL)
		return -1;
	if(pos<0 || pos > GetLengthSqlist(sq) || Sqlist_is_full(sq))
		return -1;
	int i = 0;
	for(i = sq->last;i >= pos;i--){
		sq->list[i+1] = sq->list[i];   //将元素向后移动
	}
	sq->list[pos] = data;
	sq->last++;
	return 0;
}

int DeleteSqlistBypos (sqlist * sq, int pos); // delete by location

int DeleteSqlistBypos(sqlist *sq,int pos){
	if(sq == NULL)
		return -1;
	if(pos < 0 || pos > GetLengthSqlist(sq) || Sqlist_is_empty(sq))
		return -1;
	int i = 0;
	for(i = pos;i <= sp->last;i++){
		sq->list[i] = sq->list[i+1];      //移动表内元素的值,对前面的值进行覆盖
	}
	sq->last--;  //表尾向前移动
	return 0;
}

int DeleteSqlistBydata (sqlist * sq, data_t data); // data element values ​​by deleting the table

int DeleteSqlistBydata(sqlist *sq,data_t data){
	if(sq == NULL)
		return -1;
	int i = 0;
	for(i = 0;i <= sq->last;i++){
		if(sq->list[i] == data){
			for(;i <= sq->last;i++)
				sq->list[i] = sq->list[i+1];
		}
	}
	sq->last--;
	return 0;
}

data_t SearchSqlistBypos (sqlist * sq, int pos); // by location lookup, to find the value of the element to return

data_t SearchSqlistBypos(sqlist *sq,int pos){
	if(sq == NULL)
		return -1;
	if(pos < 0 || pos > GetLengthSqlist(sq) || Sqlist_is_empty(sq))
		return -1;
	return sq->list[pos];
}

int SearchSqlistBydata (sqlist * sq, data_t data); // find the values ​​returned by the position of an element (starting from 0)

int SearchSqlistBydata(sqlist *sq,data_t data){
	if(sq == NULL || Sqlist_is_empty(sq))
		return -1;
	int i = 0;
	for(i = 0;i <= sq->last;i++){
		if(data == sq->list[i])
			return i;
	}
	if(i == sq->last+1)
		return -1;
	return 0;
}

int ChangeSqlistBypos (sqlist * sq, int pos, data_t data); // be modified by location

int ChangeSqlistBypos(sqlist *sq,int pos,data_t data){
	if(sq == NULL)
		return -1;
	if(pos < 0 || pos > GetLengthSqlist(sq) || Sqlist_is_empty(sq))
		return -1;
	sq->list[pos] = data;
	return 0;
}

int ChangeSqlistBypos (sqlist * sq, data_t old, data_t new); // value modified by

int ChangeSqlistBypos(sqlist *sq,data_t old,data_t new){
	if(sq == NULL || Sqlist_is_empty)
		return -1;
	int i = 0;
	for(i = 0;i <= sq->last;i++){
		if(old == sq->list[i])
			sq->list[i] = new;
	}
	return 0;
}

int printfSqlist (sqlist * sq); // iterate on the table

int printfSqlist(sqlist *sq){
	if(sq == NULL)
		return -1;
	int i = 0;
	for(i = 0;i <= sq->last;i++){
		printf("%3d",sq->list[i]);
	}
	printf("\n");
	return 0;
}

If wrong place, forget the comments that, thank you

Guess you like

Origin blog.csdn.net/qazxdewqs/article/details/91048981