复习_回顾所学的_顺序表


顺序表的原理


顺序表是简单的一种线性结构,逻辑上相邻的数据在计算机内的存储位置也是相邻的,可以 快速定位第几个元素,中间不允许有空值,插入、删除时需要移动大量元素。

顺序表的三个要素:
1)用 elems 记录存储位置的基地址
2)分配一段连续的存储空间 size
3) 用 length 记录实际的元素个数,即顺序表的长度

在这里插入图片描述

结构体定义

#define MAX_SIZE 100 
struct _SqList{ 
	ElemType *elems; // 顺序表的基地址 
	int length; // 顺序表的长度 
	int size; // 顺序表总的空间大小 
}

顺序表的算法实现

顺序表的初始化

#define MAX_SIZE 100 
	typedef struct{ int *elems; // 顺序表的基地址 
	int length; // 顺序表的长度 
	int size; // 顺序表的空间 
}SqList; 

bool initList(SqList &L) //构造一个空的顺序表 L 
{ 
	L.elems=new int[MAX_SIZE]; //为顺序表分配 Maxsize 个空间 
	if(!L.elems) return false; //存储分配失败 
	L.length=0; //空表长度为 0 
	L.size = MAX_SIZE; 
	return true; 
}

在这里插入图片描述
顺序表增加元素

bool listAppend(SqList &L, int e) 
{ 
	if(L.length==MAX_SIZE) return false; //存储空间已满 
	L.elems[L.length] = e; 
	L.length++; //表长增 1 
	return true; 
}

在这里插入图片描述
顺序表插入元素

bool listInsert(SqList &L,int i, int e) 
{ 
	if(i<0 || i>=L.length)return false; //i 值不合法 
	if(L.length==MAX_SIZE) return false; //存储空间已满 
	
	for(int j=L.length-1; j>=i; j--){ 
		L.elems[j+1]=L.elems[j]; //从最后一个元素开始后移,直到第 i 个元 素后移
	}
	
	L.elems[i]=e; //将新元素 e 放入第 i 个位置 
	L.length++; //表长增 1 
	return true; 
}

在这里插入图片描述
顺序表删除元素

bool listDelete(SqList &L,int i) 
{ 
	if(i<0 || i>=L.length) return false; //不合法 
	if(i == L.length-1){//删除最后一个元素,直接删除 
	L.length--; 
	return true; 
	}
	
	for (int j=i; j<L.length-1; j++){ 
		L.elems[j] =L.elems[j+1]; //被删除元素之后的元素前移 
	}

	L.length--; 
	return true; 
}

在这里插入图片描述
顺序表销毁

void destroyList(SqList &L) 
{ 
	if (L.elems) delete []L.elems;//释放存储空间 
	L.length = 0; 
	L.size = 0; 
}

参考:

#include <iostream>

using namespace std;

#define MAX_LISTSIZE 100

typedef struct sequenceList
{
	int* elems;
	int length;
	int size;
}SqList;

//初始化
bool init_sequenceList(SqList& L)
{
	L.elems = new int[MAX_LISTSIZE];
	if (!L.elems) return false;
	L.length = 0;
	L.size = MAX_LISTSIZE;
	return true;
}

//增加到尾部
bool add_to_the_tail(SqList& L, int e)
{
	if (L.length == MAX_LISTSIZE) return false;
	L.elems[L.length] = e;
	L.length++;
	return true;
}

//增加 任意位置插入
bool insert_list(SqList& L, int i, int e)
{
	if (i > L.length || i < 0) return false;
	if (L.length == MAX_LISTSIZE) return false;

	for (int j = L.length-1; j>=i; j--)
	{
		L.elems[j + 1] = L.elems[j];
	}

	L.elems[i] = e;
	L.length++;
	return true;
}

//删除
bool delete_list(SqList& L, int i)
{
	if (i > L.length || i < 0) return false;

	if (L.length - 1 == i)
	{
		L.length--;
		return true;
	}

	for (int j = i; j < L.length - 1; j++)
	{
		L.elems[j] = L.elems[j + 1];
	}

	L.length--;
	return true;
}

//打印
void printf_list(SqList& L)
{
	cout << "当前的length: " << L.length << " size: " << L.size << endl;

	for (int i = 0; i <= L.length - 1; i++)
	{
		cout << L.elems[i] << " ";
	}
	cout << endl;
}


//释放销毁
void destroy_list(SqList& L)
{
	if (L.elems) delete[] L.elems;
	L.length = 0;
	L.size = 0;
}

int main()
{
	SqList list;
	int i, e;

	cout << "初始化链表..." << endl;

	if (init_sequenceList(list))
	{
		cout << "初始化顺序表成功" << endl;
	}
	else
	{
		cout << "初始化失败" << endl;
	}


	//2. 添加元素
	int count;
	cout << "请输入你要添加的元素个数" << endl;
	cin >> count;
	for (int j = 0; j < count; j++)
	{
		cout << "请输入你要添加的元素: ";
		cin >> e;
		if (add_to_the_tail(list, e))
		{
			cout << "添加成功" << endl;
		}
		else
		{
			cout << "添加失败" << endl;
		}
	}

	printf_list(list);


	//3. 插入
	cout << "请输入你要插入的位置: ";
	cin >> i;
	cout << "请输入你要插入的元素: ";
	cin >> e;
	if (insert_list(list, --i, e))
	{
		cout << "插入成功!" << endl;
	}
	else
	{
		cout << "插入失败!" << endl;
	}
	printf_list(list);


	//删除
	cout << "请输入你要删除的位置: ";
	cin >> i;
	if (delete_list(list, --i))
	{
		cout << "删除成功" << endl;
	}
	else
	{
		cout << "删除失败" << endl;
	}
	printf_list(list);


	//销毁
	destroy_list(list);
	cout << "销毁顺序表..." << endl;
	

	system("pause");
	return 0;
}

结语:

大家对学到的知识要, 多复习, 多总结, 多敲.

我是小白, 如果存在问题, 欢迎大神给予评判指正.
错了不可怕, 可怕的是找不出bug

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weifc-wei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值