数据结构—顺序表的基本操作和实现(C++)

顺序表是采用顺序储存结构线性表

顺序表相邻的数据元素储存在相邻的物理储存单元中

顺序表的基本操作包括初始化、查找、插入、删除、交换、逆序和更改,本文以C++为例实现了这些操作

#include<iostream>

using namespace std;

#define ElemType int
#define LIST_INIT_SIZE 100   //初始化最大表长为100
#define LISTINCREMENT 10     //约定每次扩容时增加10个元素

typedef struct {
	ElemType* elem;     //本实现方式不直接储存数组,而是初始化顺序表时动态开辟数组,这里储存该数组的指针
	int length;         //顺序表元素个数
	int listsize;       //顺序表最大长度,初始为LIST_INIT_SIZE(以ElemType为单位)
	int incrementsize;  //约定每次扩表增加incrementsize个元素,以LISTINCREMENT为初始值(以ElemType为单位)
}SqList;

//顺序表初始化函数
void InitList_Sq(SqList& L) {
	L.elem = new ElemType[LIST_INIT_SIZE];
	L.length = 0;
	L.listsize = LIST_INIT_SIZE;
	L.incrementsize = LISTINCREMENT;
}

//构建顺序表,依次输入前n个数据
bool CreateList_Sq(SqList& L, int n) {
	if (n<0 || n>L.listsize) { return false; }
	L.length = n;
	for (int i = 0; i < L.length; i++) {
		cin >> L.elem[i];
	}
	return true;
}

//查找函数(查找表内是否含有特定元素,若存在该元素,则返回该序列(从1开始),未找到则返回0)
int LocateElem_sq(SqList& L, ElemType a) {
	ElemType* p = L.elem;
	for (int i = 0; i < L.length; i++) {
		if (p[i] == a) { return i + 1; }   //注意顺序表序号从1开始,而数组从0开始
	}
	return 0;
}

//插入函数(在第i个元素前插入元素e)
void increment(SqList& L);           //顺序表扩容函数的声明,定义下面给出
bool ListInsert_Sq(SqList& L, int i, ElemType e) {
	if (i<1 || i>L.length) { return false; }
	if (L.length >= L.listsize) { increment(L); }   //若表长已经等于当前允许的最大值,则顺序表扩容
	ElemType tem1, tem2;
	tem1 = e;
	for (int j = 0; j < L.length - i + 1; j++) {
		tem2 = L.elem[i + j - 1];
		L.elem[i + j - 1] = tem1;
		tem1 = tem2;
	}
	L.elem[L.length] = tem2;
	L.length++;
	return true;
}
void increment(SqList &L) {
	ElemType* p = new ElemType[L.listsize + L.incrementsize];
	for (int i = 0; i < L.length; i++) {
		p[i] = L.elem[i];
	}
	delete[] L.elem;
	L.elem = p;
	L.listsize += L.incrementsize;
 }

//删除函数(删除第i项数据)
bool DeleteElem_Sq(SqList& L, int i) {
	if (i<1 || i>L.length) { return false; }
	for (int j = i; j < L.length; j++) {
		L.elem[j - 1] = L.elem[j];
	}
	L.length--;
	return true;
}

//交换函数(交换顺序表第i项和第j项)
bool SwapList_Sq(SqList& L, int i, int j) {
	if (i<1 || i>L.length) { return false; }
	if (j<1 || j>L.length) { return false; }
	ElemType tem;
	tem = L.elem[i - 1];
	L.elem[i - 1] = L.elem[j - 1];
	L.elem[j - 1] = tem;
	return true;
}

//逆序函数(将顺序表反向排列,即第一位变为最后一位)
void ReverseList_Sq(SqList& L) {
	for (int i = 0; i < L.length / 2; i++) {
		ElemType tem;
		tem = L.elem[i];
		L.elem[i] = L.elem[L.length - i - 1];
		L.elem[L.length - i - 1] = tem;
	}
}

//更改函数(将第i个元素改为e)
bool ChangeElem_Sq(SqList& L, int i, ElemType e) {
	if (i<1 || i>L.length) { return false; }
	L.elem[i - 1] = e;
	return true;
}

//输出顺序表
void showlist(SqList& L) {
	for (int i = 0; i < L.length; i++) {
		cout << L.elem[i] << ' ';
	}
	cout << endl;
}

/*
*以下为验证并观察上述代码
*构建一个包含{1,2,3,4,5}的顺序表,并依次执行上述函数
*/
int test01() {
	SqList L;
	InitList_Sq(L);
	CreateList_Sq(L, 5); showlist(L);
	LocateElem_sq(L, 3); showlist(L);
	ListInsert_Sq(L, 2, 100); showlist(L);
	DeleteElem_Sq(L, 2); showlist(L);
	SwapList_Sq(L, 1, 5); showlist(L);
	ReverseList_Sq(L); showlist(L);
	ChangeElem_Sq(L, 1, 100); showlist(L);
	return 0;
}
/*
*依次输出
*1 2 3 4 5
*1 2 3 4 5
*1 100 2 3 4 5
*1 2 3 4 5
*5 2 3 4 1
*1 4 3 2 5
*100 4 3 2 5
*/

/*
* 以下验证increment函数
*/
int test02() {
	SqList L;
	InitList_Sq(L);
	for (int i = 0; i < L.listsize; i++) {
		L.elem[i] = i;
	}
	L.length = 100;
	ListInsert_Sq(L, 1, 100);
	showlist(L);
	return 0;
}
/*
* 输出(手动换行)
* 100 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
* 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
* 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
* 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值