数据结构与算法之数组顺序表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a844651990/article/details/84579402

  最近打算把数据结构与算法重新复习一遍,边复习边写博客方便以后查看,先来最简单的数组顺序表。

顺序表

  顺序表是在计算机内存中以数组的形式保存的线性表,线性表的顺序存储是指用一组地址连续的存储单元依次存储线性表中的各个元素、使得线性表中在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中,即通过数据元素物理存储的相邻关系来反映数据元素之间逻辑上的相邻关系,采用顺序存储结构的线性表通常称为顺序表。顺序表是将表中的结点依次存放在计算机内存中一组地址连续的存储单元中。

实现

我们要实现的基本操作有,其它的操作可自行扩展:

  1. 初始化: 给线性表中的相关参数赋值
  2. 销毁: 销毁线性表
  3. 清空: 清空线性表
  4. 判断是否为空
  5. 求长度: 求线性表中的元素的个数
  6. 取元素: 取给定位置的元素值
  7. 查元素: 查找给定元素值的位置
  8. 插入元素: 在指定的位置插入给定的值
  9. 删除: 删除指定位置的值或者是删除给定的值。
  10. 遍历元素: 从头到尾扫描线性表。

源码

#include <stdio.h>
#include <stdlib.h>

typedef int ElemType; // 假定线性表的元素类型为整型
#define LIST_SIZE 1024  // 假定我们的线性表长度是1024
#define TRUE 1
#define FALSE 0

typedef struct {
	ElemType data[LIST_SIZE];
	int length;
}SequenList;

/*
1)	初始化:  给线性表中的相关参数赋值
2)  销毁:    销毁线性表
3)  清空:    清空线性表
4)  判断是否为空
5)	求长度:  求线性表中的元素的个数
6)	取元素:  取给定位置的元素值
7)	查元素:  查找给定元素值的位置
8)	插入元素: 在指定的位置插入给定的值
9)	删除:    删除指定位置的值或者是删除给定的值。
10)	遍历元素: 从头到尾扫描线性表。
*/

/*
 * 初始化: 给线性表中的相关参数赋值
 *		  长度为0
 * 返回值: SequenList *, 初始化完成的表
 */
SequenList *initSeq()
{
	SequenList *pList = NULL;
	pList = (SequenList *)malloc(sizeof(SequenList));
	if (pList != NULL)
		pList->length = 0;
	return pList;
}

/*
 * 销毁: 销毁线性表
 */
void destroyList(SequenList *pList)
{
	if (pList == NULL)
		return;

	free(pList);
	pList = NULL;
}

/*
 * 清空: 清空线性表
 */
void clearList(SequenList *pList)
{
	if (pList == NULL)
		return;

	pList->length = 0;
}
/*
 * 判断是否为空
 * 为空返回TRUE,不为空返回FALSE,否则返回-1
 */
int listEmpty(SequenList *pList)
{
	if (pList == NULL)
		return -1;

	if (pList->length == 0)
		return TRUE;

	if (pList->length > 0)
		return FALSE;
}

/*
 * 求长度: 求线性表中的元素的个数
 * 参数:   pList, 顺序表
 * 返回值: list有效返回真实长度,无效时返回 -1
 */
int getSizeOfList(SequenList *pList)
{
	if (pList != NULL)
		return pList->length;
	else
		return -1;
}

/*
 * 取元素:   取给定位置的元素值
 * 参数: pList, 顺序表
 *	    pos, 元素的位置
 *      elem, 找到的元素
 * 返回值: 找到时返回TRUE,未找到返回FALSE
 */
int getElemOfList(SequenList *pList, int pos, ElemType *elem)
{
	if (pList == NULL)
		return FALSE;
	if (pos < 0 || pos > pList->length || pList->length <= 0)
		return FALSE;

	*elem = pList->data[pos];
	return TRUE;
}

/*
 * 查元素: 查找给定元素值的位置, 
 * 参数:   pList, 顺序表; elem, 要查找的元素
 * 返回值: 返回找到的第一个元素的位置索引;未找到或表无效时返回-1
 */
int indexOfElem(SequenList *pList, ElemType elem)
{
	if (pList == NULL)
		return -1;
	for (int i = 0; i < pList->length; ++i) {
		if (pList->data[i] == elem)
			return i;
	}
	return -1;
}

/*
 * 插入元素: 在指定的位置插入给定的值
 * 参数:   pList, 顺序表; elem, 要插入的元素; pos, 要插入的位置
 * 返回值: 插入成功返回TRUE,否则返回FALSE
 */
int insertElem(SequenList *pList, ElemType elem, int pos)
{
	if (pList == NULL)
		return FALSE;

	// 确认顺序表是否已经填满了
	if (pList->length > LIST_SIZE)
		return FALSE;
	// 位置无效
	if (pos < 0 || pos > pList->length)
		return FALSE;

	for (int i = pList->length; i < pos; --i) {
		pList->data[i + 1] = pList->data[i];
	}
	pList->data[pos] = elem;
	pList->length++;
	return TRUE;
}

/*
 * 删除:  删除指定位置的值。
 * 参数:   pList, 顺序表; pos, 要删除的元素的位置
 * 返回值: 删除成功返回TRUE,否则返回FALSE
 */
int deleteElem(SequenList *pList, int pos)
{
	if (pList == NULL)
		return FALSE;
	if (pos < 0 || pos >(pList->length - 1) || pList->length == 0)
		return FALSE;

	if (pList->length == 1) {
		pList->data[0] = 0;
		pList->length--;
		return TRUE;
	}
		
	for (int i = pos; i < (pList->length-1); ++i) {
		pList->data[i] = pList->data[i + 1];		 
	}
	pList->length--;
	return TRUE;
}

/*
 * 遍历元素: 从头到尾扫描线性表。
 */
void showSeqList(SequenList *pList)
{
	if (pList == NULL)
		return;

	for (int i = 0; i < pList->length; ++i) {
		printf(" %d", pList->data[i]);
	}
}

int main()
{
	// 初始化
	SequenList *pList = initSeq();
	
	if (pList != NULL) {
		// 插入数据
		for (int i = 0; i < 10; ++i) {
			insertElem(pList, i, i);
		}
		// 遍历顺序表
		showSeqList(pList);
		// 获取大小
		printf("\nlist size: %d\n", getSizeOfList(pList));
		// 删除数据
		deleteElem(pList, 1);
		showSeqList(pList);
		printf("\nlist size: %d", getSizeOfList(pList));

		// 取出给定位置的元素
		int elem; 
		getElemOfList(pList, 4, &elem);
		printf("\npos 4: %d", elem);

		// 查找元素的位置
		elem = 9;
		int pos = indexOfElem(pList, elem);
		printf("\nelem 9 pos: %d", pos);
	}
	
	getchar();
	destroyList(pList);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/a844651990/article/details/84579402
今日推荐