数据结构-顺序表的基本操作代码实现

(利用假期时间复习一下数据结构)

#include <iostream>
using namespace std;

typedef int ElemType;
const int LIST_INIT_SIZE = 10;
const int INCREMENTLIST = 10;
typedef struct {
	ElemType* data;
	int length;
	int listsize;
}SqList;

bool InitList(SqList& S);//顺序表初始化
bool CreatList(SqList& S, int n);//构造顺序表,对顺序表赋值,赋值数量为n
bool EmptyList(SqList S);//判断顺序表是否为空
bool ListInsert(SqList& S, int i, ElemType e);//在顺序表第i位插入元素e
bool ListRemove(SqList& S, int i, ElemType& e);//移除顺序表第i位的元素,并用e返回
bool GetElem(SqList S, int i, ElemType& e);//用元素e返回顺序表第i位的元素
int SearchList(SqList S, ElemType e);//寻找元素e在顺序表中首次出现的位置,并返回位置编号
int ListLength(SqList S);//返回顺序表长度
bool PriorElem(SqList S, int i, ElemType& Pre);//寻找顺序表第i位元素的前驱元素,用Pre返回
bool NextElem(SqList S, int i, ElemType& Ne);//寻找顺序表第i位元素的后继元素,用Ne返回
bool ReInit(SqList& S);//当顺序表被占满后,动态分配顺序表内存空间
void ClearList(SqList& S);//清空顺序表
void DestoryList(SqList& S);//销毁顺序表
void TraverseList(SqList S);//遍历顺序表

bool InitList(SqList& S) {
	S.data = new ElemType[LIST_INIT_SIZE];
	if (!S.data)return 0;
	S.length = 0;
	S.listsize = LIST_INIT_SIZE;
	return 1;
}
bool CreatList(SqList& S, int n) {
	cout << "请输入" << n << "个元素构建顺序表:" << endl;
	while (S.listsize < n)
		ReInit(S);
	for (int i = 0; i < n; i++) {
		cin >> S.data[i];
		S.length++;
	}
	return 1;
}
bool EmptyList(SqList S) {
	if (S.length == 0)
		return 1;
	return 0;
}
bool ListInsert(SqList& S, int i, ElemType e) {
	if (i<1 || i>S.length + 1)
		return 0;
	if (S.length == S.listsize)
		ReInit(S);
	for (int j = S.length; j >= i; j--)
		S.data[j] = S.data[j - 1];
	S.data[i - 1] = e;
	S.length++;
	return 1;
}
bool ListRemove(SqList& S, int i, ElemType& e) {
	if (i<1 || i>S.length + 1)
		return 0;
	for (int j = i; j < S.length; j++)
		S.data[j - 1] = S.data[j];
	S.length--;
	return 1;
}
bool GetElem(SqList S, int i, ElemType& e) {
	if (i<1 || i>S.length)
		return 0;
	e = S.data[i - 1];
	return 1;
}
int SearchList(SqList S, ElemType e) {
	for (int i = 0; i < S.length; i++) 
		if (S.data[i] == e)
			return i - 1;
	return -1;
}
int ListLength(SqList S) {
	return S.length;
}
bool PriorElem(SqList S, int i, ElemType& Pre) {
	if (i<1 || i>S.length)
		return 0;
	if (i == 1)
		return 0;
	Pre = S.data[i - 1 - 1];
	return 1;
}
bool NextElem(SqList S, int i, ElemType& Ne) {
	if (i<1 || i>S.length)
		return 0;
	if (i == S.length)
		return 0;
	Ne = S.data[i - 1 + 1];
	return 1;
}
bool ReInit(SqList& S) {
	ElemType* newdata = new ElemType[S.listsize + INCREMENTLIST];
	if (!newdata)return 0;
	for (int i = 0; i < S.listsize; i++)
		newdata[i] = S.data[i];
	ElemType* p = S.data;
	S.data = newdata;
	delete[] p;
	S.listsize += INCREMENTLIST;
	return 1;
}
void ClearList(SqList& S) {
	S.length = 0;
}
void DestoryList(SqList& S) {
	delete[] S.data;
	S.data = NULL;
}
void TraverseList(SqList S) {
	for (int i = 0; i < S.length; i++)
		cout << S.data[i] << "  ";
	cout << endl;
}

int main()
{
	SqList S;
	ElemType e;
	int n, i;
	if (InitList(S))
		cout << "初始化顺序表成功!" << endl;
	else {
		cout << "初始化顺序表失败!" << endl;
		return 0;
	}
	cout << "请输入线性表中元素个数:";
	cin >> n;
	if (!CreatList(S, n)) {
		cout << "顺序表赋值失败!" << endl;
		return 0;
	}
	if (EmptyList(S))//检验线性表是否为空
		cout << "线性表为空" << endl;
	else
	{
		cout << "线性表不为空" << endl;
		cout << "线性表中元素个数为" << ListLength(S) << endl;//查询线性表中元素个数
		TraverseList(S);//遍历
		cout << "请输入要查询的线性表元素的位序:";//查询指定位序元素
		cin >> i; 
		if (GetElem(S, i, e))
			cout << "线性表中第" << i << "个元素为" << e << endl;
		else
			cout << "输入位序超过线性表长度" << endl;
		cout << "请输入要查询的元素:";//查询指定元素位序
		cin >> e; 
		int orderID = SearchList(S, e);
		if (orderID!=-1)
			cout << "线性表中元素" << e << "为第" << orderID << "个" << endl;
		else
			cout << "输入元素未在表中" << endl;
		cout << "请输入要查询前驱的元素:";//查找前驱
		ElemType Pe;
		cin >> e; 
		if (PriorElem(S, e, Pe))
			cout << "线性表中元素" << e << "的前驱为" << Pe << endl;
		else
			cout << "输入元素未在表中找到前驱" << endl;
		cout << "请输入要查询后继的元素:";//查找后继
		ElemType Ne;
		cin >> e; 
		if (NextElem(S, e, Ne))
			cout << "线性表中元素" << e << "的后继为" << Ne << endl;
		else
			cout << "输入元素未在表中找到后继" << endl;
	}
	cout << "请输入线性表插入元素的位序及插入元素值:";//查询指定位序元素
	cin >> i >> e; 
	if (ListInsert(S, i, e))
	{
		cout << "线性表插入元素成功" << endl;
		cout << "线性表中元素个数为" << ListLength(S) << endl;//查询线性表中元素个数
		TraverseList(S);//遍历
	}
	else
		cout << "线性表插入元素失败" << endl;
	if (!EmptyList(S))//检验线性表是否为空
	{
		cout << "请输入线性表删除元素的位序:";//查询指定位序元素
		cin >> i; 
		if (ListRemove(S, i, e))
		{
			cout << "线性表删除元素成功" << endl;
			cout << "线性表中元素个数为" << ListLength(S) << endl;//查询线性表中元素个数
			TraverseList(S);//遍历
		}
		else {
			cout << "线性表删除元素失败" << endl;
		}
	}
	if (!EmptyList(S))//检验线性表是否为空
	{
		ClearList(S);
		cout << "线性表已置空" << endl;
	}
	DestoryList(S);
	cout << "线性表已销毁" << endl;
	return 0;
}

 输出样例

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

菜菜的大鹏

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

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

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

打赏作者

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

抵扣说明:

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

余额充值