基础数据结构——定长顺序表

线性表

1)顺序表

a. 定长顺序表的实现

首先是其结构体的定义部分:

#define N 10
typedef int ELEMTYPE;

//定长顺序表
typedef struct Sqlist
{
	ELEMTYPE arr[N];
	int length;
}Sqlist,*PSqlist;

首先是其结构体的定义部分:

接下来是简单函数的定义:

//初始化
void Init_sqlist(PSqlist plist)
{
	assert(plist != NULL);
	
	plist->length = 0;
}
//打印
void Print_sqlist(PSqlist plist)
{
	assert(plist != NULL);
	
	for (int i = 0; i < plist->length; i++)
	{
		printf("%d ", plist->arr[i]);
	}
	printf("\n");
}
//按位置插入
bool Insert_pos(PSqlist plist, int pos, int val)
{
	assert(plist != NULL);
	assert(pos >= 0 && pos <= plist->length);

	for (int i = plist->length - 1; i >= pos; i--)
	{
		plist->arr[i + 1] = plist->arr[i];
	}
	plist->arr[pos] = val;
	plist->length++;
	return true;
}
//按值删除
bool Del_val(PSqlist plist, int val)
{
	assert(plist != NULL);
	//是否空
	if (IsEmpty(plist))
	{
		return false;
	}

	int pos = Search_val(plist, val);
	if (pos == -1)	return false;
	else
	{
		Del_pos(plist, pos);
	}
	return true;
}
//按位置删除
bool Del_pos(PSqlist plist, int pos)
{
	assert(plist != NULL);
	assert(pos > 0 && pos <= plist->length);

	for (int i = pos; i < plist->length - 1; i++)
	{
		plist->arr[i] = plist->arr[i + 1];
	}
	plist->length--;
	return true;
}
//查找val的结点
int Search_val(PSqlist plist, int val)
{
	assert(plist != NULL);
	
	for (int i = 0; i < plist->length; i++)
	{
		if (plist->arr[i] == val)
			return i;
	}
	return -1;
}
//判空
bool IsEmpty(PSqlist plist)
{
	assert(plist != NULL);

	if (plist->length == 0)	return true;
	else
	{
		return false;
	}
}
//判满
bool IsFull(PSqlist plist)
{
	assert(plist != NULL);

	if (plist->length == N)	return true;

}
//清空
bool Clear(PSqlist plist)
{
	assert(plist != NULL);

	plist->length = 0;
	return true;
}
//销毁
bool Destory(PSqlist plist)
{
	assert(plist != NULL);

	//Clear(plist);
	free(plist);
	return true;
}

上一篇:实验三 面向对象程序设计


下一篇:统计页码数字