C++实现顺序表基本操作

#include<iostream>
using namespace std;

#define InitSize 10       //10不会直接写,会写的更通用一点
#define IncreaSize 2

typedef int ElemType;    //typedef: 类型的重命名  struct  --> SqList    int --> ElemType

//使用结构体对顺序表进行表示
typedef struct 
{
	ElemType* data;    //指向这个结构体的指针变量,存储空间动态申请

	int length;       //顺序表当前长度

	int MaxSize;     //顺序表最大长度

}SqList;


//1 初始化顺序表
bool InitList(SqList &L)
{
	//首先需要动态分配一个地址
    //注意这个函数参数的位置,这里可以写成SqList *L,下面必须写成L->elem或者(*L).elem,
    //如果是Sqlist &L 下面写的形式是L.elem(*L 表示的是指针,而&L是引用,说人话就是二者都可以
   // 指向这个顺序表,&L可以理解为一个别名而已。先这样理解吧)
 
	L.data = new ElemType[InitSize];
	if (L.data == NULL)
		return false;
	L.length = 0;
	L.MaxSize = InitSize;
	return true;
}

//2 向顺序表中存入数据
void Input(SqList&L, ElemType n)
{
	if (n > InitSize || n <= 0)
		cout << "输入错误,请重新输入:" << endl;
	cout << "请输入" << n << "个数据" << endl;
	for (int i = 0; i < n; i++)
	{
		cin >> L.data[i];
	}
	L.length = n;
}

//3 线性表的长度
ElemType GetLength(SqList& L)
{
	return L.length;
}

//4 遍历顺序表中的数据
void Show(SqList& L)
{
	cout << "顺序表内存储的数据为:" << endl;
	for (int i = 0; i < L.length; i++)
	{
		cout << L.data[i] << " ";
	}
	cout << endl;
}

//5 向顺序表指定位置插入数据
bool InsertList(SqList& L, ElemType k, ElemType e)
{
	if (L.length == InitSize)
		cout << "顺序表已存满" << endl;
	if (k<1 || k>L.length+1)
		return false;
	cout << "在顺序表第" << k << "个位置插入数据" << e << "后" << endl;
	for (int i = L.length-1; i >= k-1; i--)
	{
		L.data[i+1] = L.data[i];
	}
	L.data[k-1] = e;
	L.length++;
	return true;
}

//6 删除线性表指定位置数据
bool DeleteList(SqList& L, ElemType k)
{
	if (k<1 || k>L.length)
		return false;
	cout << "删除顺序表中第" << k << "个数据后" << endl;
	for (int i = k-1; i < L.length; i++)
	{
		L.data[i] = L.data[i + 1];
	}
	L.length--;
	return true;
}

//7 返回顺序表中第i个数据元素的值
void GetElem(SqList& L, ElemType i)
{
	if (i > L.length || i <= 0)
		cout << "输入错误,请重新输入:" << endl;
	cout << "第" << i << "个数值为" << L.data[i - 1] << endl;
}

//8 返回查找数据在顺序表中的位置
void LocateList(SqList& L)
{
	int e,f;
	if (L.length == 0)
	{
		cout << "当前顺序表为空" << endl;
	}
	else
	{
		cout << "请输入查询的数值:" << endl;
		cin >> e;

		for (int i = 0; i < L.length; i++)
		{
			if (L.data[i] == e)
			{
				f = i;
				break;
			}
			else
				f = -1;
		}
		if (f == -1)
		{
			cout << "顺序表中不存在查询值" << endl;
		}
		else
			cout << "查找数据在顺序表中的位置为" << f << endl;
	}
}

//9 动态增加顺序表长度
void IncreaList(SqList&L)
{
	ElemType* p = L.data;
	L.data = new ElemType[InitSize + IncreaSize];
	for (int i = 0; i < L.length; i++)
	{
		L.data[i] = p[i];
	}
	L.MaxSize = L.MaxSize + IncreaSize;
	delete p;
}

//10 清空线性表中数据,重置为空表
bool ClearList(SqList& L)
{
	if (L.length == 0)
	{
		cout << "顺序表为空" << endl;
	}
	for (int i = 0; i < L.length; i++)
	{
		L.data[i] = 0;
	}
	L.length = 0;
	cout << "顺序表已清空" << endl;
	return true;
}

//11 销毁线性表
bool DestoryList(SqList& L)
{
	if (L.data != NULL)
	{
		free(L.data);						//首先释放地址空间
		L.data = NULL;					   //指针指向空
		L.length = 0;					  //长度均变成0
		cout << "顺序表已销毁" << endl;
		return true;
	}
	else
	{
		cout << "顺序表不存在" << endl;
		return false;
	}
}

//12 合并两个顺序表
//待续...

int main()
{
	SqList L;

	InitList(L);
	Input(L, 6);
	Show(L);

	InsertList(L, 3, 9999);
	Show(L);

	DeleteList(L, 2);
	Show(L);

	GetElem(L, 4);
	LocateList(L);

	ClearList(L);
	Show(L);

	DestoryList(L);

	system("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值